2017-03-03 21 views
1

無論如何,我一直在尋找一個tkinter函數,詢問用戶一個多項選擇題,我發現最接近的是messagebox.asknoyes,但它只提供了2個選擇,此外我不能編輯選擇,因爲它們是固定的(是或否),是否有一個tkinter函數可以滿足我的要求?如何顯示使用tkInter詢​​問用戶多項選擇的對話框?

注意:這不是Taking input from the user in Tkinter的可能重複,因爲該問題詢問如何從用戶那裏獲得輸入,因此用戶可以提交他們想要的任何輸入,同時我想給用戶一些預定義的選擇來選擇一個

+3

我不認爲有一個內置的功能。我想你將不得不手動創建一個窗口,手動添加單選按鈕和標籤,等待用戶響應,然後手動檢查選中哪個單選按鈕。 – Kevin

+0

@Kevin,謝謝你,請讓這個答案讓我可以接受它[在Tkinter的來自用戶的輸入採取]的 – ArandomUserNameEG

+0

可能的複製(http://stackoverflow.com/questions/15495559/taking-input-from-用戶在tkinter) –

回答

3

我不認爲有一個內置的功能。我想你將不得不手動創建一個窗口,手動添加單選按鈕和標籤,等待用戶響應,然後手動檢查選中哪個單選按鈕。

幸運的是這很簡單,所以我爲你做了一個快速實施。

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))) 
相關問題