首页 > 解决方案 > 被代码忽略的函数,如果写在另一个函数中

问题描述

我编写了一个新的 Function1 (data_city),如果插入到另一个 Function2 (write) 中,它将被忽略。我不明白这个问题。以前我删除了 Function1 (data_city) 并将内容复制/粘贴到 Function1 (data_city) 的 IF 中:这样一切正常,一切正常。但是,如果我创建 (data_city) 函数并在 write 函数中调用它,则 data_city 函数将被忽略并且不起作用。我从 Python 开始,对不起。你能告诉我解决问题的代码吗?

我知道该函数被忽略了,因为我没有与我附加的第二段代码相同的结果(带有 name_city、cursor.execute 和结果在 write 函数的 IF 内的代码)。由于 text.insert (tk.END, f "{name_city} {'' .join (word2)} {inhabitants}resident on a area of​​ {surface}"),文本应该会打印在文本框中。另一方面,data_city 函数用于获取与在组合框 city.get 中选择的项目相对应的数据库的整行

为了对好心的读者提出问题的完整性、实用性和清晰性,我发布了给我带来问题的代码段,过去有效的代码段,以及完整的代码。非常感谢会回答的人

def data_city():
    name_city = city.get()
    cursor.execute('SELECT * FROM TableExample WHERE Name_city=?',(name_city,))
    results = cursor.fetchone()


def write():

    results = data_city()

    if categoria.get() == "test1" and sottocategoria.get() == "test2":
   
        cursor.execute('SELECT Test1 FROM TableExample ORDER BY RANDOM() LIMIT 1')
        word2 = cursor.fetchone()

        inhabitants = results[2]
        surface = results[3]

        text.delete(1.0,END)
        text.insert(tk.END, f"{name_city} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}")

以前,只有一个 write 函数的代码,以及我复制并粘贴到 wriste 函数中的 data_city 函数的内容,如下所示。它工作正常,一切正常:

def write():
    if categoria.get() == "test1" and sottocategoria.get() == "test2":

        name_city = city.get()
        cursor.execute('SELECT * FROM TableExample WHERE Name_city=?',(name_city,))
        results = cursor.fetchone()

        cursor.execute('SELECT Test1 FROM TableExample ORDER BY RANDOM() LIMIT 1')
        word2 = cursor.fetchone()

        inhabitants = results[2]
        surface = results[3]

        text.delete(1.0,END)
        text.insert(tk.END, f"{name_city} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}")

出于对读者的应用程序的清晰性、实用性和完整性的原因,我附上应用程序的整个代码以用于教育目的:

from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3

window=Tk()
window.title("aaaaa")
window.geometry("750x750")
window.configure(bg='#78c030')

con = sqlite3.connect('/home/mypc/Scrivania/aaaa/Database.db')
cursor = con.cursor()

### PULSANTI ###

def write():
    if categoria.get() == "test1" and sottocategoria.get() == "test 1.2":

        name_city = city.get()
        cursor.execute('SELECT * FROM TableExample WHERE Name_city=?',(name_city,))
        results = cursor.fetchone()

        cursor.execute('SELECT Test1 FROM TableExample2 ORDER BY RANDOM() LIMIT 1')
        word2 = cursor.fetchone()

        inhabitants = results[2]
        surface = results[3]

        text.delete(1.0,END)
        text.insert(tk.END, f"{name_city} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}")


button2 = Button(window, text="Button2", bg='white', command = write)
button2.pack()
button2.place(x=5, y=330)

### TEXTBOX MULTILINE ###
text = Text(window,width=63,height=38)
text.pack()
text.place(x=180, y=24)

### CATEGORIA E SOTTO CATEGORIA ###
cat=StringVar()
sub_cat=StringVar()

def change_val(*args):
    if cat.get() == "test1":
        sottocategorias = ["test 1.1", "test 1.2", "test 1.3"]
        sottocategoria.config(values=sottocategorias)
    else:
        sottocategorias = ["aaaa"]
        sottocategoria.config(values=sottocategorias)

categorias=["test1", "test2", "test3"]
categoria=ttk.Combobox(window,value=categorias,
textvariable=cat,width=16)
categoria.place(x=5, y=25)
cat.set("Scegliere categoria")

sottocategorias=["aaaa"]
sottocategoria=ttk.Combobox(window,textvariable=sub_cat,
value=sottocategorias,width=16)
sottocategoria.place(x=5, y=55)

cat.trace("w",change_val) 

### COMBOBOX ###

### CAMPIONATO COMBOBOX ###
def combo_nation():
    cursor.execute('SELECT DISTINCT Nation FROM TableExample')
    result=[row[0] for row in cursor]
    return result

### SQUADRA COMBOBOX ###
def combo_city(event=None):
    val = nation.get()
    cursor.execute('SELECT Name_city FROM Info WHERE TableExample = ?', (val,))
    result = [row[0] for row in cursor]
    city['value'] = result
    city.current(0)
    return result

nation=ttk.Combobox(window,state="readonly")
nation['value'] = combo_campionati()
nation.bind('<<ComboboxSelected>>', combo_squadre)
nation.place(x=5, y=150,height = 25, width = 180)

city=ttk.Combobox(window,state="readonly")
city.place(x=5, y=180, height = 25, width = 180)

window.mainloop()

标签: pythonpython-3.xfunctionpython-2.7tkinter

解决方案


由于data_city()返回一个字符串,因此单独运行它不会做任何事情。你需要results = data_city()write()函数中做。

您的代码应如下所示:

def data_city(name_city):
    cursor.execute('SELECT * FROM TableExample WHERE Name_city=?',(name_city,))
    results = cursor.fetchone()
    return results

def write():
    name_city = city.get()
    results = data_city(name_city)

    if categoria.get() == "test1" and sottocategoria.get() == "test2":
        cursor.execute('SELECT Test1 FROM TableExample ORDER BY RANDOM() LIMIT 1')
        word2 = cursor.fetchone()

        inhabitants = results[2]
        surface = results[3]

        text.delete(1.0,END)
        text.insert(tk.END, f"{name_city} {''.join(word2)} {inhabitants} inhabitants on an area of {surface}")

推荐阅读