Python 如何在执行命令之前使tkinter按钮升起
我正在尝试制作GUI应用程序,在将输入的数字打印到终端之前,您必须选择一个Python 如何在执行命令之前使tkinter按钮升起,python,button,tkinter,Python,Button,Tkinter,我正在尝试制作GUI应用程序,在将输入的数字打印到终端之前,您必须选择一个predict按钮,如果未选择任何一个predict按钮,则应打印出选择predict按钮以继续操作 在pseudecode中,类似于: def check: now = new.get() if no lotto_button is selected: print("select predict button to proceed") else: print(now
predict
按钮,如果未选择任何一个predict
按钮,则应打印出选择predict按钮以继续操作
在pseudecode中,类似于:
def check:
now = new.get()
if no lotto_button is selected:
print("select predict button to proceed")
else:
print(now)
但面临挑战:
1:当我选择一个预测按钮时,两个按钮都会升起
2:如何在打印条目中的内容之前选择预测
import tkinter as tk
def terminal():
now = new.get()
print(now)
def start_func():
lotto_button.configure(relief=tk.SUNKEN, state=tk.DISABLED)
lotto_button1.configure(relief=tk.SUNKEN, state=tk.DISABLED)
def stop_func():
lotto_button.configure(relief=tk.RAISED, state=tk.ACTIVE)
lotto_button1.configure(relief=tk.RAISED, state=tk.ACTIVE)
root = tk.Tk()
root.geometry("400x400")
new = tk.StringVar()
en = tk.Entry(root, textvariable=new).pack()
lotto_button = tk.Button(root, text="predict one", command=start_func)
lotto_button.pack(side="left")
lotto_button1 = tk.Button(root, text="predict two", command=start_func)
lotto_button1.pack()
tk.Button(root, text="print number", command=lambda :[stop_func(),
terminal()]).place(x=150, y=300)
root.mainloop()
谢谢你,欢迎你的建议 当您单击其中一个按钮时,这两个按钮都会被触发,因为在函数start\u func中,您同时配置了这两个按钮。 您可以绑定一个事件单击并在start_func中获取小部件,而不是同时配置这两个小部件,如下所示:
lotto_button = tk.Button(root, text="predict one")
lotto_button.bind('<1>', start_func)
lotto_button.pack(side="left")
关于你的第二点,我不太明白你的意思。至于强制拾取,你可以在最外层的范围内定义一个标志(与
root=tk.tk()处于同一级别),这样你就可以在函数中看到它是否设置了:
isPredicted = False
至于在选择一个按钮时禁用每个按钮,您可以定义您的start\u func
,使其以按钮对象(lotto\u button
或lotto\u button1
)作为参数:
def start_func(button_object):
global isPredicted
isPredicted = True
button_object.configure(relief=tk.SUNKEN, state=tk.DISABLED)
lotto_button.configure(command=lambda button_object=lotto_button: start_func(button_object))
lotto_button1.configure(command=lambda button_object=lotto_button1: start_func(button_object))
要使用此函数,您需要按以下方式配置按钮,以便它们将自身作为参数传递:
def start_func(button_object):
global isPredicted
isPredicted = True
button_object.configure(relief=tk.SUNKEN, state=tk.DISABLED)
lotto_button.configure(command=lambda button_object=lotto_button: start_func(button_object))
lotto_button1.configure(command=lambda button_object=lotto_button1: start_func(button_object))
您的stop_func
中也应该有isPredicted
标志,以便它将其取消设置:
def stop_func():
global isPredicted
isPredicted = False
lotto_button.configure(relief=tk.RAISED, state=tk.ACTIVE)
lotto_button1.configure(relief=tk.RAISED, state=tk.ACTIVE)
对于您的终端
功能,您希望根据isPredicted
的状态进行操作,因此需要将其定义为:
def terminal():
global isPredicted
if isPredicted:
now = new.get()
print(now)
else:
print("select predict button to proceed")
最后,在您的“打印号码”
按钮中,您需要交换lambda中函数的顺序,或者isPredicted
就按钮的命令而言始终为False:
tk.Button(root, text="print number", command=lambda :[terminal(),
stop_func()]).place(x=150, y=300)
您的最终代码应该如下所示:
import tkinter as tk
from tkinter import messagebox
def terminal():
global isPredicted
if isPredicted:
now = new.get()
print(now)
else:
print("select predict button to proceed")
def start_func(button_object):
global isPredicted
isPredicted = True
button_object.configure(relief=tk.SUNKEN, state=tk.DISABLED)
def stop_func():
global isPredicted
isPredicted = False
lotto_button.configure(relief=tk.RAISED, state=tk.ACTIVE)
lotto_button1.configure(relief=tk.RAISED, state=tk.ACTIVE)
root = tk.Tk()
root.geometry("400x400")
new = tk.StringVar()
en = tk.Entry(root, textvariable=new).pack()
isPredicted = False
lotto_button = tk.Button(root, text="predict one")
lotto_button.configure(command=lambda button_object=lotto_button: start_func(button_object))
lotto_button.pack(side="left")
lotto_button1 = tk.Button(root, text="predict two")
lotto_button1.configure(command=lambda button_object=lotto_button1: start_func(button_object))
lotto_button1.pack()
tk.Button(root, text="print number", command=lambda :[terminal(),
stop_func()]).place(x=150, y=300)
root.mainloop()
使用Radiobutton
widgets往往更适合这种选择题处理
Radiobutton
s属于“组”,这意味着您可以设置它们,以便在默认情况下始终需要选择一个。这意味着您不需要任何类型的错误捕获或向用户显示任何类型的指令,它只是直观地工作
from tkinter import *
root = Tk()
def command(v, entry):
print(v.get(), entry.get())
v = StringVar()
v.set(1)
entry = Entry(root)
rb1 = Radiobutton(root, text="Predict One", variable=v, value=1, indicatoron=0)
rb2 = Radiobutton(root, text="Predict Two", variable=v, value=2, indicatoron=0)
button = Button(root, text="Print Number", command=lambda:command(v, entry))
entry.pack()
rb1.pack()
rb2.pack()
button.pack()
root.mainloop()
因此,我们创建了一个StringVar()
,用于定义哪些Radiobutton
小部件应该链接在一起,然后将其值设置为1
,这是“预测一个”
按钮的值。如果用户按下另一个单选按钮
,则值变为2
,这意味着第一个按钮“取消按下”,而StringVar()
的值变为2
我在第二点中的意思是,如果没有选择任何预测按钮,则应打印“选择“预测”按钮继续“在我的终端中,在您选择其中一个来打印条目小部件中的号码之前,请先中断打印过程。哦,我现在明白了。检查Nae的答案,这是非常完整的:)你可能应该为此使用小部件。但我想问你的是,我如何才能使一次只升起一个按钮,因为我可以同时选择两个按钮,它将被激活raised@NewpyBoi美好的正如其他人所提到的,单选按钮可能工作得更好。我们尝试过提供的示例@Ethan字段,但在选择它并打印为单选按钮设置的值之前,它会对小部件应用提升效果。当我删除这些值并选择其中一个单选按钮时,它会同时选择两个。@NewpyBoi您可以将将terminal
中的if-else
语句与您的start\u func
相同,并将除global-isPredicted
之外的所有内容放入else
中。