【问题标题】:Python: Call a Function1 inside Function2Python:在 Function2 中调用 Function1
【发布时间】:2021-06-15 13:49:37
【问题描述】:

我想拆分这个函数的代码,推出代码(name_city = city.get() ... cursor.execute ...., and results)。我附上两个代码:第一个应用程序运行良好,而第二个我想获得但我不能。

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}")

现在如果我想从内部消除我们(name_city = city.get () ... cursor.execute .... and results),创建函数 def data_city,以便将其插入函数(def写),不是里面if,而是out,怎么办?我已经尝试过了,但它不起作用。问题是def data_city函数好像被忽略了怎么调用write函数里面的data_city函数? (但在 If 之外,不在 If 之内)。谢谢

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


def write():

    data_city() #ERROR/PROBLEM

    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}")

为了更加完整和清晰,我附上了完整的可启动代码

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()

【问题讨论】:

  • 究竟什么“不起作用”?例如什么是堆栈跟踪?
  • 您未能重现代码的实际缩进。我们不知道函数的代码或if 语句的主体在哪里结束。这样做很容易——粘贴代码,全选,然后按编辑器工具栏上的{} 按钮。
  • 删除了 python-2.7python-3.x,因为它不是特定于 Python2 或 Python3。而且您显然使用的是 Python3.6+,因为您使用的是 f-strings。欢迎来到 StackOverflow。了解如何提供Minimal, Reproducible Example 来帮助我们解决您的问题并阅读How to ask a good question
  • @sophros 该应用程序会定期启动。不起作用的是 data_city 函数。就好像它不存在一样。它不会被回调
  • @aneroid 感谢您的欢迎。好的,现在我读了。再次感谢

标签: python python-3.x python-2.7


【解决方案1】:

看起来您缺少 data_city 函数的返回值?试试:

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

【讨论】:

  • 试过了。那不好。可能我在write函数里面调用data city()函数是错误的。调用它,只需要写data_city()?
  • 我修改了问题,添加了更多细节。我已经使问题更清晰,更完整。我还添加了更多代码。我希望你能帮助我。谢谢
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
相关资源
最近更新 更多