【发布时间】:2021-09-01 14:35:52
【问题描述】:
我编写了一个新的 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 中选择的项目相对应的数据库的整行。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()
【问题讨论】:
-
请阅读formatting help 并确保您的代码显示的格式与您的格式完全相同。您在此处显示的代码中有许多缩进错误,这将使其根本无法在 Python 中运行,而且通常无法猜测您实际上是如何嵌套的。
-
不管怎样,当你说函数“被忽略”时,你怎么知道?当你说它“不起作用”时,应该发生什么没有发生?当您运行该程序时,您究竟看到了什么,这与您期望看到的到底有什么不同?
-
看来你具体问的是你在哪里有
inhabitants = results[2];您期望它使用来自data_city的return的results。它不能以这种方式工作。函数没有return变量;它们返回值,如果您希望调用者为该值命名,那么它必须分配自己的名称。 -
@Karl Knechtel 我知道该函数被忽略了,因为我没有与我附加的第二段代码相同的结果(带有 name_city、cursor.execute 和结果的那个)写函数的 IF)。由于 text.insert (tk.END, f "{name_city} {'' .join (word2)} {inhabitants}resident on a area of {surface}"),文本应该会打印在文本框中。当我运行程序时,有 4 个组合框、一个按钮和一个大的多行 texbox。
-
@Kark Knechtel 评论太长了。我把它分成两个答案。该程序的功能是在文本框中打印一些文本行,由 text.insert(tk.END, f "{name_city} {'' .join (word2)} {inhabitants} 区域内的居民生成{表面} ”)。文本取自数据库。
标签: python python-3.x function python-2.7 tkinter