GUI로 tkinter를 사용하는 텍스트 기반 모험이 있는데, 다른 옵션으로 버튼에 레이블을 붙이고 어떤 버튼을 반환 할 함수를 만들려고합니다. 눌렀다. 이 작업을 수행하여 게임의 모든 결정에 대한 일반적인 기능으로 사용할 수 있습니다. 단추를 만들 수 있지만 클릭 한 단추를 반환하는 함수를 가져올 수 없습니다. 이것은 대략 내가 지금까지 가지고있는 것입니다 :tkinter에서 클릭 한 버튼에 따라 함수의 반환 값을 설정하는 방법
from tkinter import *
import time
class Main(Frame):
def __init__(self, master):
Frame.__init__(self, master)
#configuring and placing frame
def change_1(self):
self.choice = 1
..............
def change_5(self):
self.choice = 5
def userChoice(self, o1, o2, o3, o4, o5, title, cmdlist)# cmdlist if there is less than 5 options
self.choice = 0
titleLable = Label(self, text = title)
#place & config
button_1 = Button(self, text = o1, command = self.change_1)
#place and configure buttons
..........
button_5 = Button(self, text = o5, command = self.change_5)
#place and configure more buttons
while self.choice not in cmdlist:
time.sleep(.5)
return self.choice
root = Tk()
main = Main(root)
cmdlist = [1, 2, 3, 4, 5]
cmd = main.createChoices("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Choose an Option", cmdlist)
#here would be something similar to: if cmd == 1:
do this etc.
root.mainloop()
도움이되었지만 여전히 루프를 시도하고있는 동안 GUI가 표시되지 않도록하는 것처럼 값을 검색하는 데 문제가 있습니다 – idka
tkinter에서 무한 루프를 사용할 수 없지만, 처리가 완료 될 때까지 프로그램을 멈추게하고 해결할 수없는 무한한 상태가됩니다. 'mainloop'은 이미 무한 루프이므로 다른 것을 가질 수 없습니다. 나는 당신이 무엇을 하려는지 확실하지 않지만 백그라운드에서 가치를 확인하는 대신에 생각합니다. 함수 안에서 버튼을 클릭하면 원하는 것을하십시오. 또는'root.after'를 사용하여 설정 시간 후에 함수를 계속해서 다시 호출 할 수 있습니다. –
감사합니다.'root.after'는 꿈처럼 움직였습니다. – idka