Python Tkinter简单值过程



我只是GUI的新手,不需要什么帮助。

a=int(input())
if a==0:
print("hi")
else:
print("hello")

我想把输入过程改为点击按钮,像开关一样。

左键->a=0

右按钮->a=1

window=tkinter.Tk()
window.title("")
window.geometry("640x640+100+100")
window.resizable(True, True)

a=tkinter.Button(window, text="left")
a.pack()
b=tkinter.Button(window, text="right")
b.pack()
window.mainloop()

我可以看到左、右按钮,但我不知道如何设置值。

有什么我可以用的例子吗?

感谢

这个例子对你有帮助吗:

from tkinter import Tk, Button

def switch(btn1, btn2):
btn1.config(state='disabled')
btn2.config(state='normal')
print(btn1['text'])

window = Tk()
window.title("")
on = Button(window, text="On", command=lambda: switch(on, off))
on.pack(side='left', expand=True, fill='x')
off = Button(window, text="Off", command=lambda: switch(off, on))
off.pack(side='left', expand=True, fill='x')
off.config(state='disabled')
window.mainloop()

如果你有问题问,但这里有一个很好的网站来查找tkinter小部件以及它们的功能和属性。

我还建议你遵循PEP 8

您需要为每个按钮添加一个函数,当点击按钮时将执行该函数:

import tkinter as tk
def left_clicked():
print("Left button clicked")
right_button.config(state="normal") # Reset the button as normal
left_button.config(state="disabled") # Disable the button
def right_clicked():
print("Right button clicked")
right_button.config(state="disabled")
left_button.config(state="normal")
window = tk.Tk()
window.title("")
window.geometry("640x640+100+100")
# window.resizable(True, True) # Unneeded as it is already the default
left_button = tk.Button(window, text="left", command=left_clicked)
left_button.pack(side="left")
right_button = tk.Button(window, text="right", command=right_clicked,
state="disabled")
right_button.pack(side="right")
window.mainloop()

相关内容

最新更新