我能请求一点帮助吗?我创建了一个带有切换按钮的GUI,用于切换LED,LED熄灭.
我现在要做的是添加一些代码来更改按钮的文本,因为它在两个状态之间切换.
我查了一些例子但是我不太清楚如何或者在哪里添加代码来使按钮文本切换.
谢谢你的帮助.
我的代码......
# Idle 07_02_LED ON using GUI
from time import sleep
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=1, column=0)
button = Button(frame, text='LED 0 ON', command=self.convert0)
button.grid(row=2, columnspan=2)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('LED 0 OFF')
else:
print('LED 0 ON')
root = Tk()
root.wm_title('LED on & off program')
app = App(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 我有一个带有 Piface 适配器板的 Raspberry Pi。我制作了一个 GUI 来控制 Piface 板上的 LED。GUI 上的一个按钮会打开一个新窗口,按下按钮即可启动和停止运行一小段代码,以使 LED 连续上下运行,就像 Knight Riders 汽车一样,在线程中使用 While 循环。在这个新窗口中,我添加了一个退出按钮。我想添加一段代码,当我单击“退出”按钮时,该代码将关闭新窗口,然后返回到主窗口。我查了很多例子,但不太明白我应该放什么或放哪里。我尝试过“退出”,但它关闭了整个程序。看了很多例子后,我可能以不太正确的方式创建新窗口,所以请随时告诉我是否有更好的方法。
那么有没有更好的方法呢?任何指示将不胜感激。
提前致谢。
这是一段代码......
def new_window(self):
print('New Window')
self.newWindow = tk.Toplevel(self.master)
self.app = App2(self.newWindow)
self.newWindow.grab_set() # I added this line to stop opening multiple new windows
class App2:
def __init__(self, master):
frame = Frame(master)
frame.pack()
Label(frame, text='Turn LED ON').grid(row=0, column=0)
Label(frame, text='Turn LED OFF').grid(row=0, column=1)
self.button0 = Button(frame, text='Knight Rider OFF', command=self.convert0)
self.button0.grid(row=2, column=0)
self.LED0 = Label(frame, image=logo2)
self.LED0.grid(row=2, column=1)
self.button9 = Button(frame, …Run Code Online (Sandbox Code Playgroud)