[英]How to make a query from a selected choice from the user from a drop-down list in Django
[英]Prompt user for input if item on drop-down list is selected in Python
我在 python 中有一個 GUI,除其他外,讓用戶從下拉菜單中選擇項目(我使用了 tkinter 的組合框功能)。
我希望擁有它,以便在選擇“自定義”項時,會出現一個輸入框,詢問用戶他們的自定義編號是什么。 我不想使用按鈕來顯示框,但不知何故,只要選擇自定義,輸入框就會出現。 我首先嘗試使用 while 循環,但無法使其正常工作,與 if 語句相同:s
我還嘗試使用我在這里找到的 askinteger() 方法( http://effbot.org/tkinterbook/tkinter-entry-dialogs.htm ),但我想出的組合都沒有奏效(我對 Python 和我還在學習所以請原諒我明顯的錯誤)。
這是我的 GUI 代碼:
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from tkinter import StringVar
from tkinter import messagebox
from tkinter import simpledialog
class MyGUI:
def __init__(self, master):
self.master = master
master.title("Intent/Interpretation Check")
self.runlabel = Label(master, text="RunID :")
self.runlabel.grid(row=0, column=0)
self.runentry = Entry(master)
self.runentry.grid(row=1, column=0, padx=25)
self.checklabel = Label(master, text="Check type :")
self.checklabel.grid(row=0, column=1)
self.typeselect = Combobox(master)
self.typeselect['values']=("Intent Score", "Interpretation Score")
self.typeselect.grid(row=1, column=1, padx=25)
self.limitlabel = Label(master, text="Fails if score is below :")
self.limitlabel.grid(row=0, column=2, padx=25)
self.limitselect = Combobox(master)
self.limitselect['values']=(1000, 5000, "Custom")
self.limitselect.grid(row=1, column=2, padx=25)
if self.limitselect.get() != "Custom":
self.limit = self.limitselect.get()
pass
else:
self.askinteger("Custom limit", "Please enter a number from 1 to 10000", minvalue=1, maxvalue=10000)
self.submitbutton = Button(master, text="Submit", command=self.checkstatus)
self.submitbutton.grid(row=1, column=3, padx=25, pady=5)
root = Tk()
root.geometry("+600+300")
my_gui = MyGUI(root)
root.mainloop()
非常感謝您提前!
您需要有一個 Bool 來告訴何時應該顯示新輸入。 您還需要不斷輪詢 ComboBox 以查看它的值是否等於“自定義”。 這是我在大約 3 分鍾內想到的。
我沒有試圖讓 GUI 看起來很漂亮,只是一個功能性的例子。
from tkinter import *
from tkinter.ttk import *
class Gui:
def __init__(self):
self.root = Tk()
# Set up the Combobox
self.selections = Combobox(self.root)
self.selections['values'] = ['Apples', 'Oranges', 'Blueberries', 'Bananas', 'Custom']
self.selections.pack()
# The Entry to be shown if "Custom" is selected
self.custom_field = Entry(self.root)
self.show_custom_field = False
# Check the selection in 100 ms
self.root.after(100, self.check_for_selection)
def check_for_selection(self):
'''Checks if the value of the Combobox equals "Custom".'''
# Get the value of the Combobox
value = self.selections.get()
# If the value is equal to "Custom" and show_field is set to False
if value == 'Custom' and not self.show_custom_field:
# Set show_field to True and pack() the custom entry field
self.show_custom_field = True
self.custom_field.pack()
# If the value DOESNT equal "Custom"
elif value != 'Custom':
# Set show_field to False
self.show_custom_field = False
# Destroy the custom input
self.custom_field.destroy()
# Set up a new Entry object to pack() if we need it later.
# Without this line, tkinter was raising an error for me.
# This fixed it, but I don't promise that this is the
# most efficient method to do this.
self.custom_field = Entry(self.root)
# If the value IS "Custom" and we're showing the custom_feild
elif value == 'Custom' and self.show_custom_field:
pass
# Call this method again to keep checking the selection box
self.root.after(100, self.check_for_selection)
app = Gui()
app.root.mainloop()
希望這可以幫助!
編輯:
要打開一個新窗口而不是將其打包在與 Combobox 相同的窗口中,請將函數check_for_selection
替換為:
def check_for_selection(self):
value = self.selections.get()
# If the value is equal to "Custom" and show_field is set to False
if value == 'Custom' and not self.show_custom_field:
# Set show_field to True and pack() the custom entry field
self.show_custom_field = True
# Create a new window how we did when we made self.root
self.new_window = Tk()
# Create the Entry that will go in the window. The previous Entry widget from line 16, can be removed
self.custom_field = Entry(self.new_window)
self.custom_field.pack()
# Run the new window like we did the original
self.new_window.mainloop()
# If the value DOESNT equal "Custom"
elif value != 'Custom':
# Destroy the new window that was created if it exists
if self.show_custom_field:
self.new_window.destroy()
# Set show_field to False
self.show_custom_field = False
# If the value IS "Custom" and we're showing the custom_feild
elif value == 'Custom' and self.show_custom_field:
print('yes')
# Call this method again to keep checking the selection box
self.root.after(100, self.check_for_selection)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.