저는 스스로 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 ()'함수를 어떻게 만들 수 있는지, 아니면 파이썬에 이미 존재하는 것이 있다면 나에게 말할 수 있습니까?
감사합니다!
모든 GUI (모든 언어)에서 버튼을 사용하여 입력을 기다리고 입력 데이터가있을 때 일부 기능을 실행합니다.
에서 tkinter
당신은 사용이 lambda
버튼 인수 기능을 assing에
self.b1 = tk.Button(self.window, text='Option 1', command=lambda:self.choose('1'))
나는 after()
지연으로 텍스트를 변경하고 지연으로 버튼을 추가하는 데 사용 callback
했습니다 say_later()
. 지연으로 실행하는 데 사용 했습니다 .
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()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다