Я учу Python самостоятельно и сейчас пытаюсь выучить немного GUI с помощью Tkinter. Если кто-то может помочь, у меня возникли проблемы с тем, чтобы программа показала приветственное сообщение, а затем предложила варианты, которые нужно выбрать, нажав кнопку. На самом деле, я думаю, что я разобрался, как его построить, но отсутствует ключевой элемент: я не знаю, как заставить программу ждать, пока пользователь не взаимодействует, нажав одну из кнопок.
Скажем, например, я создаю файл с классом «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?
Оцените это!