如果在Python中选择了下拉列表上的项,则提示用户输入

2024-06-16 13:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中有一个GUI,它让用户从下拉菜单中选择项目(我使用了tkinter的combobox特性)。在

我希望有它,以便当项目“自定义”被选中时,一个输入框会出现,询问用户他们的自定义编号是什么。我不想使用一个按钮来显示框,但不知何故,只要选择了自定义,输入框就会出现。我首先尝试了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()

提前非常感谢!在


Tags: textfromimportselfmasteriftkintercolumn
1条回答
网友
1楼 · 发布于 2024-06-16 13:22:39

您需要有一个Bool来告诉何时显示新的输入。 您还需要不断地轮询组合框,看看它的值是否等于“Custom”。这是我在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()

希望这有帮助!在

编辑:

要打开一个新窗口而不是将其打包到与组合框相同的窗口中,请将函数check_for_selection替换为:

^{pr2}$

相关问题 更多 >