切换 LED Python 3.x tkinter
Toggle LED Python 3.x tkinter
我正在尝试使用 GUI 打开和关闭 LED。
当我执行代码时,我得到一个空白框,标题为 "tk"。
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
self.button.grid(row=2, columnspan=2)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
self.button.config(text='LED 0 OFF')
else:
self.button.config(text='LED 0 ON')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
您的代码需要一些修复。
首先,运行 它给了我以下错误:
NameError: name 'Label' is not defined
可以肯定的是,事实并非如此。
定义的是tk.Label
,所以让我们改变这两行:
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
进入:
tk.Label(frame, text='Turn LED ON').grid(row=0, column=0)
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0)
现在,我出现了以下错误:
NameError: name 'frame' is not defined
果然,它也不是。
您可能指的是 Application
class 扩展了 tk.Frame
class。
好吧,这是真的,但这并没有告诉我们 frame
是什么。
我假设 frame
表示 "the instance, but considered as a Frame
instance"。
在这种情况下,self
就足够了(实际上是需要的)。
所以我们开始吧,以下三行:
tk.Label(frame, text='Turn LED ON').grid(row=0, column=0)
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
成为:
tk.Label(self, text='Turn LED ON').grid(row=0, column=0)
tk.Label(self, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(self, text='LED 0 ON', command=self.convert0)
现在,有人告诉我
NameError: name 'Button' is not defined
我相信您已经开始明白这一点了。
所以让我们用 tk.Button
:
替换 Button
self.button = tk.Button(self, text='LED 0 ON', command=self.convert0)
现在开始,显示 window,带有两个标签和一个漂亮的按钮,单击时其文本会发生变化。
我正在尝试使用 GUI 打开和关闭 LED。
当我执行代码时,我得到一个空白框,标题为 "tk"。
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
self.button.grid(row=2, columnspan=2)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
self.button.config(text='LED 0 OFF')
else:
self.button.config(text='LED 0 ON')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
您的代码需要一些修复。
首先,运行 它给了我以下错误:
NameError: name 'Label' is not defined
可以肯定的是,事实并非如此。
定义的是tk.Label
,所以让我们改变这两行:
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
进入:
tk.Label(frame, text='Turn LED ON').grid(row=0, column=0)
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0)
现在,我出现了以下错误:
NameError: name 'frame' is not defined
果然,它也不是。
您可能指的是 Application
class 扩展了 tk.Frame
class。
好吧,这是真的,但这并没有告诉我们 frame
是什么。
我假设 frame
表示 "the instance, but considered as a Frame
instance"。
在这种情况下,self
就足够了(实际上是需要的)。
所以我们开始吧,以下三行:
tk.Label(frame, text='Turn LED ON').grid(row=0, column=0)
tk.Label(frame, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(frame, text='LED 0 ON', command=self.convert0)
成为:
tk.Label(self, text='Turn LED ON').grid(row=0, column=0)
tk.Label(self, text='Turn LED OFF').grid(row=1, column=0)
self.button = Button(self, text='LED 0 ON', command=self.convert0)
现在,有人告诉我
NameError: name 'Button' is not defined
我相信您已经开始明白这一点了。
所以让我们用 tk.Button
:
Button
self.button = tk.Button(self, text='LED 0 ON', command=self.convert0)
现在开始,显示 window,带有两个标签和一个漂亮的按钮,单击时其文本会发生变化。