gpt4 book ai didi

python - 如何让脚本等待用户在 Tkinter 中交互?

转载 作者:行者123 更新时间:2023-12-01 06:31:43 24 4
gpt4 key购买 nike

我正在自学 Python,现在我正在尝试使用 Tkinter 学习一些 GUI。如果有人可以提供帮助,我将无法让程序显示问候消息,然后提供应通过单击按钮选择的选项。实际上,我想我已经弄清楚了如何构建它,但缺少一个关键部分:我不知道如何让程序等到用户通过单击其中一个按钮进行交互。

比方说,我创建一个带有“Window”类(用于界面)和一些函数(Window.py)的文件:


import time
from tkinter import *

# Here I'll set each one of the the buttons' commands.
# I've referenced it before to avoid triggering a reference problem, but when I tried, I actually broke the code in three parts and used imports to link all of them.
# For simplicity's sake, however, I'll present everything in a single file in this question.

Choice = ''

def Choose1():
Choice = 1

def Choose2():
Choice = 2

def Choose3():
Choice = 3

# Here I create the object 'Window':

class Window:

def __init__(self):
self.Window = Tk()
self.Window.title = 'Interact'
self.Window.minsize = (500, 300)
# Here I'll add a label to display the messages that I want to show the user:
self.Text = Label(self.Window, text = '')
self.Text.pack()
# Aqui uma série de botões:
self.B1 = Button(self.Window, text = 'Option 1', command = Choose1)
self.B1.pack()
self.B2 = Button(self.Window, text = 'Option 2', command = Choose2)
self.B2.pack()
self.B3 = Button(self.Window, text = 'Option 3', command = Choose3)
self.B3.pack()

# Here I'll create an instance of the 'Window' object:

Example = Window()

# Here I'll create a function so that certain messages will be displayed to the user:

def Say(X):
Example.Text.configure(text = X)
Example.Text.update()
time.sleep(3) # Please ignore this. I inserted this delay so there's time for the user to read the message. I actualy have a better way to do it, but to keep it simple, let's leave it like this.

# Finally, the main part of the program:

Say('Welcome!')
Say('Which option would you like to choose?')
WaitInput() # I haven't figured out how this function would work, and that's my issue. I'd like the program to wait for the option to be chosen and only then print the following message:
Say('You've chosen option {}!'.format(Choose))

Text.mainloop()

任何人都可以告诉我如何创建这个“WaitInput()”函数,或者 Python 中是否已经存在此类函数?

欣赏!

最佳答案

在所有 GUI(所有语言)中,您都可以使用按钮等待输入并在有输入数据时执行某些功能。

tkinter中,您必须使用lambda来将带有参数的函数传递给按钮

self.b1 = tk.Button(self.window, text='Option 1', command=lambda:self.choose('1'))
<小时/>

我使用 after() 延迟更改文本,并添加延迟按钮 - 我在 say_later() 中使用 callback 来延迟执行。

import time
import tkinter as tk

# --- classes ---

class Window:

def __init__(self):
self.window = tk.Tk()
self.window.title = 'Interact'
self.window.geometry('500x300')
#self.window.minsize = (500, 300)

self.text = tk.Label(self.window, text='')
self.text.pack()

def add_buttons(self):
self.b1 = tk.Button(self.window, text='Option 1', command=lambda:self.choose('1'))
self.b1.pack()
self.b2 = tk.Button(self.window, text='Option 2', command=lambda:self.choose('2'))
self.b2.pack()
self.b3 = tk.Button(self.window, text='Option 3', command=lambda:self.choose('3'))
self.b3.pack()

def say(self, message, callback=None):
self.text.configure(text=message)
if callback:
callback()

def say_later(self, delay, message, callback=None):
self.window.after(delay, lambda:self.say(message, callback))

def choose(self, value):
self.say("You've chosen option {}!".format(value))

# --- functions ---

# empty

# --- main ---

example = Window()

example.say('Welcome!')
example.say_later(3000, 'Which option would you like to choose?', example.add_buttons)

example.window.mainloop()

关于python - 如何让脚本等待用户在 Tkinter 中交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59878433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com