我不認爲有一個內置的功能。我想你將不得不手動創建一個窗口,手動添加單選按鈕和標籤,等待用戶響應,然後手動檢查選中哪個單選按鈕。
幸運的是這很簡單,所以我爲你做了一個快速實施。
from tkinter import Tk, Label, Button, Radiobutton, IntVar
# ^Use capital T here if using Python 2.7
def ask_multiple_choice_question(prompt, options):
root = Tk()
if prompt:
Label(root, text=prompt).pack()
v = IntVar()
for i, option in enumerate(options):
Radiobutton(root, text=option, variable=v, value=i).pack(anchor="w")
Button(text="Submit", command=root.destroy).pack()
root.mainloop()
if v.get() == 0: return None
return options[v.get()]
result = ask_multiple_choice_question(
"What is your favorite color?",
[
"Blue!",
"No -- Yellow!",
"Aaaaargh!"
]
)
print("User's response was: {}".format(repr(result)))
我不認爲有一個內置的功能。我想你將不得不手動創建一個窗口,手動添加單選按鈕和標籤,等待用戶響應,然後手動檢查選中哪個單選按鈕。 – Kevin
@Kevin,謝謝你,請讓這個答案讓我可以接受它[在Tkinter的來自用戶的輸入採取]的 – ArandomUserNameEG
可能的複製(http://stackoverflow.com/questions/15495559/taking-input-from-用戶在tkinter) –