tkinter - 不能从另一个函数中调用一个函数
问题描述
嗨,我正在尝试使用 tkinter 创建一个带有按钮的页面,当按下该按钮时,该页面上的条目框的状态将从只读更改为正常。
这是我的代码,其中按钮调用的函数在我的脚本仍在运行的类中,但按钮什么也不做:
class playlist(tkinter.Frame): #creates playlist page
def __init__(self, parent, controller):
tkinter.Frame.__init__(self, parent)
A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Edit", command=lambda: edit)
E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)
E1.pack()
A4.pack(fill="both", expand=True)
AC.pack()
B1.pack()
B2.pack()
A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
A4.create_window(960, 80, anchor="center", window=E1)
A4.create_window(650, 80, anchor="center", window=B2)
def edit():
E1.configure(state = "normal")
B2.configure(text="Done")
E1.pack()
B2.pack()
App.update_idletasks()
这样函数就不能定义 E1 或 B2 所以我尝试了一个函数在第一个函数中的版本:
class playlist(tkinter.Frame): #creates playlist page
def __init__(self, parent, controller):
tkinter.Frame.__init__(self, parent)
A4 = tkinter.Canvas(self, bg="black", bd=20, height=1080, width=1920, relief="sunken") #creates black background
AC = tkinter.Label(self, bg="white", bd=5, height=45, width=225,) #creates white canvas
B1 = tkinter.Button(self, anchor="center", height=1, width=6, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Home", command=lambda: controller.show_frame(homepage))
B2 = tkinter.Button(self, anchor="center", height=1, width=4, bg="#16EBF2", activebackground="#23F216", relief="sunken",
font=("Elephant", 24), text="Edit", command=lambda: edit)
E1 = tkinter.Entry(self, state="readonly", text="Name:", bd=5, bg="red", font=("Elephant", 24), exportselection=0)
E1.pack()
A4.pack(fill="both", expand=True)
AC.pack()
B1.pack()
B2.pack()
A4.create_window(960, 550, anchor="center", window=AC) #puts new canvas on background
A4.create_window(200, 80, anchor="center", window=B1) #adds buttons to canvas
A4.create_window(960, 80, anchor="center", window=E1)
A4.create_window(650, 80, anchor="center", window=B2)
def edit():
E1.configure(state = "normal")
B2.configure(text="Done")
E1.pack()
B2.pack()
App.update_idletasks()
这样脚本仍然运行,按钮仍然什么都不做。没有错误只是不工作。提前致谢 :)
解决方案
我的主要建议是先看看没有 tkinter 的 python 类,然后在回到它之前熟悉构建一个类。
基本上,您需要将 tkinter 小部件分配给类的属性,以便可以从类中的其他任何地方引用它们。IE。当您创建必须稍后在edit
方法中引用的按钮时,请将它们分配给属性:
self.B1 = tkinter.Button(self, ...)
self.B2 = tkinter.Button(self, ...)
然后,为了让您的edit
方法能够稍后访问它们,将类实例self
传递给它,因此您必须为您的编辑方法提供一个self
参数,即。
def edit(self):
# then you can reference your attributes which store the widgets
self.B1.config(...)
最后,按钮的命令self.B2
只需要引用edit
方法即可,不需要 lambda 语句,即。
Button(...command=self.edit)
推荐阅读
- python - 如何使用 python 在下面绘制?
- java - 为什么 sdkman 不会更改我的默认 java 版本?
- javascript - 出现错误:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:%s.%s%s,未定义,
- typescript - 打字稿将参数解包到重载函数
- sql - 在另一个 ComboBox 值更改后更改 ComboBox 的数据源
- flutter - 只改变一个屏幕方向颤动
- wpf - CEFSharp 的渲染问题
- node.js - 从嵌入式文档中删除最近的对象?
- excel - Excel VBA计数突出显示的字数
- javascript - 使用单选按钮在 javascript 中设置脚本