【问题标题】:tkinter option menu doesn't display selectiontkinter 选项菜单不显示选择
【发布时间】:2014-11-04 17:14:20
【问题描述】:

我有一个使用选项菜单的 python/tkinter 程序可以正常工作,但是在第二个窗口(主窗口打开并且是单独的 tk 执行的窗口)中,选择的选项不会出现在选项菜单中。我已经测试过,该选项被选中,我可以使用它,它只是不显示。

主窗口调用以下类:

class ftp_window:
    def __init__(self):
        root = Tk()
        root.resizable(width=FALSE, height=FALSE)
        root.title("Unity Acesso ao FTP")
        root.iconbitmap("icon.ico")
        note = Notebook(root)
        self.tab1 = Frame(note)
        self.tab2 = Frame(note)
        note.add(self.tab1, text = "Acessar Bases")
        note.add(self.tab2, text = "ANALISE")
        root.minsize(600,500)
        root.geometry("600x500")
        self.ftp_apelido = StringVar()
        self.ftp_apelido.set("Select")
        self.varvoz = StringVar()
        self.tab_one()
        note.pack()
        root.mainloop()

    def get_list_of_apelidos(self):
        '''sqlite3 connection for ftp connections'''
        conn = sqlite3.connect("settings.db")
        c = conn.cursor()
        c.execute("select * from ftp_server_login")
        results = c.fetchall()
        list = ()
        for line in results:
            if not re.search(line[4], str(list)):
                list += (line[4],)
            else:
                pass
        return list


    def prints(self, *args):
        '''used to display the stringvar when selected.'''  
        print args
        print self.ftp_apelido.get()


    def tab_one(self):
        '''creates the first tab and it's widgets'''
        main_tab_one_frame = Frame(self.tab1)
        main_tab_one_frame.config(width=300)
        main_tab_one_frame.grid(column=0, row=0, sticky=NSEW, rowspan=10, columnspan=10)
        apelidos_Entry = OptionMenu(main_tab_one_frame, self.ftp_apelido, self.ftp_apelido.get(),  *self.get_list_of_apelidos())
        apelidos_Entry.config(width=25)
        apelidos_Entry.grid(column=3, row=3)
        vozEntry=OptionMenu(main_tab_one_frame, self.varvoz, '', '', 'SIM', 'NAO')
        vozEntry.config(width=15)
        vozEntry.grid(column=1, row=4)

【问题讨论】:

    标签: python python-2.7 tkinter optionmenu


    【解决方案1】:

    如果您要创建多个Tk 实例,这至少是问题的一部分。您应该只为整个应用程序创建一个 Tk 实例,并且您应该只调用一次 mainloop。如果您需要更多窗口,您应该创建Toplevel 的实例。

    【讨论】:

    • 你是真实的,只有在一个充满天才的房间里。这完美地解决了我的问题。 muito obrigado。 :)
    猜你喜欢
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2023-04-07
    • 2012-07-19
    相关资源
    最近更新 更多