我是python语言的新手 只是想要一个python程序的帮助,我在很多热门和试用之后设法写了。
from time import sleep
from Tkinter import *
def qf(par):
print(par)
class App:
def __init_(self, master):
frame = Frame(self, master)
frame.pack()
self.button = Button(frame, text='LED ON', command=self.convert0)
self.button.grid(row=5, coloumnspan=5)
def convert0(self, tog[0]):
tog[0] = not tog[0]
if tog[0]:
self.button.config(text='LED OFF')
else:
self.button.config(text='LED OFF'. command=lambda:qf("1!!!"))
root = Tk()
root.wm_title('LED Prog')
app = App(root)
root.mainloop()
程序的输出是当我点击“LED开启”按钮时。它超越了价值' 1 !!!'按钮卡在' LED ON',不会更改为' LED OFF'。
我想要的是按钮是' LED ON'那个时间1 !!!!应该显示在输出中,当按钮是“LED关闭”时不应显示任何值。
我知道这可能是python专家的一个小代码更改,但在这里需要一些帮助。 任何人都可以分享一些学习python的链接。
提前致谢。 请不要阻止这个问题 我正在使用python 2.7.9
答案 0 :(得分:1)
使用button.configure(text=)
更改文字,在OFF
和ON
之间切换,使用变量保存状态。
from time import sleep
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text='LED ON', command=self.convert0)
self.button.pack()
self.state = 1
def convert0(self):
states = ['ON', 'OFF']
self.state = not self.state
# True == 1 and False == 0
self.button.configure(text='LED {0}'.format(states[not self.state]))
root = Tk()
root.wm_title('LED Prog')
app = App(root)
root.mainloop()
使用grid
方法:
grid(row=, column=) then you can give `rowspan` or `columnspan`.
答案 1 :(得分:0)
如果我理解你的话,那就是你要找的东西:
from Tkinter import *
class App:
def __init__(self, master):
""" The constructor consists of a a root widget to which
is attached one Button child widget.
"""
frame = Frame(master)
frame.pack()
self.led = Button(frame, fg="red", text="Start", width=15,command=self.button_state)
self.led.pack() # Use pack() if no other widgets are planned to take place
def button_state(self):
""" Switch the button text from LED OFF to LED ON: 1
At the start of the program, the button text is set to 'Start'.
"""
if self.led["text"] == "LED OFF" or self.led["text"] == "Start":
self.led["text"] ="LED ON: 1"
else:
self.led["text"]="LED OFF"
root = Tk()
root.wm_title('LED Prog')
app = App(root)
root.mainloop()
<强>结果:强>
单击按钮将在以下两种状态之间切换:
答案 2 :(得分:0)
终于得到了我所寻求的希望,这也有助于其他人。
from time import sleep
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text='LED ON', command=self.convert0)
self.button.grid(row=2, column=0)
self.LED = Label(frame).grid(row=2, column=1)
def convert0(self, tog=[0]):
tog[0] = not tog[0]
if tog[0]:
print('1')
self.button.config(text='LED OFF')
else:
self.button.config(text='LED ON')
root = Tk()
root.wm_title('LED on & off program')
app = App(root)
root.mainloop()
这完全符合我的要求。
@WoLy感谢您的努力,但我一直在寻找。