我的想法是利用下拉框来调用另一文件中的函数
先放上调用函数与函数在一个文件的代码
from tkinter import * # 大致意思是当下拉框选择‘3’时,生成3个输入框
from tkinter.ttk import * # 当下拉框选择‘4’时,生成4个下拉框
# 这是a.py 文件
def choose(self):
global but
if var.get() =='4':
but=Entry(root)
but.place(relx=0.67, rely=0.41, relheight=0.06, relwidth=0.2)
else:
but.destroy()
root = Tk()
root.geometry('380x380')
Entry(root).place(relx=0.01, rely=0.41, relheight=0.06, relwidth=0.2)
Entry(root).place(relx=0.23, rely=0.41, relheight=0.06, relwidth=0.2)
Entry(root).place(relx=0.45, rely=0.41, relheight=0.06, relwidth=0.2)
var = StringVar()
comb = Combobox(root, textvariable=var, values=['3', '4']) # 下拉框 只有3和4
comb.current(0)
comb.place(relx=0.23, rely=0.35, relheight=0.06, relwidth=0.2)
comb.bind('<<ComboboxSelected>>',choose)
root.mainloop()
现在把函数和主程序分开
from tkinter import * # 这是a.py
from tkinter.ttk import *
from b import choose
root = Tk()
root.geometry('380x380')
Entry(root).place(relx=0.01, rely=0.41, relheight=0.06, relwidth=0.2)
Entry(root).place(relx=0.23, rely=0.41, relheight=0.06, relwidth=0.2)
Entry(root).place(relx=0.45, rely=0.41, relheight=0.06, relwidth=0.2)
var = StringVar()
comb = Combobox(root, textvariable=var, values=['3', '4']) # 下拉框 只有3和4
comb.current(0)
comb.place(relx=0.23, rely=0.35, relheight=0.06, relwidth=0.2)
comb.bind('<<ComboboxSelected>>',choose('<<ComboboxSelected>>',var,root))
#上面这行我不会写,是错的,但我不会改
root.mainloop()
from tkinter import * # 这是b.py
def choose(event,var,root):
global but
if var.get() =='4':
but=Entry(root)
but.place(relx=0.67, rely=0.41, relheight=0.06, relwidth=0.2)
else:
if var.get() =='3':
but.destroy()
结果就是调用函数调用不了,哪位大佬知道怎么做,能帮我改改吗?