tkinter选项菜单不显示选择

时间:2014-11-04 17:14:21

标签: python python-2.7 tkinter optionmenu

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

主窗口调用以下类:

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)

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我正面临这个问题,即选择的值没有通过其他方法传递,该方法使用它来翻译

import tkinter as tk
from tkinter import ttk
from translate import Translator

TEXT_FIELD = '#B0BEC5'

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title('Test')
        # self.config(bg=DARK_MODE)
        self.geometry('1080x900')

        # Main Parent Frame
        self.main_frame = tk.Frame(self, relief='ridge')
        self.main_frame.grid(row=0, column=0)


        # Language selection frame
        self.lang_select_frame = tk.Frame(self.main_frame, relief='groove')
        self.lang_select_frame.grid(row=1, column=0)

        # IO frame
        self.input_frame = tk.Frame(self.main_frame, relief='groove')
        self.input_frame.grid(row=2, column=0)

        # Button frame
        self.button_frame = tk.Frame(self.main_frame, relief='groove')
        self.button_frame.grid(row=3, column=0)


        """Calling the methods"""
        self.LangSelect()
        self.IO()
        self.Button()

    def LangSelect(self):
        available_languages = ['Autodetect',
                               'Hindi',
                               'English',
                               'French',
                               'Russian']
        self.from_language = tk.StringVar()
        self.from_language.set(available_languages[0])
        from_language_menu = tk.OptionMenu(self.lang_select_frame, self.from_language, *available_languages)
        from_language_menu.grid(row=0, column=0)
        print(self.from_language)

        self.to_language = tk.StringVar()
        self.to_language.set(available_languages[0])
        to_language_menu = tk.OptionMenu(self.lang_select_frame, self.to_language, *available_languages)
        to_language_menu.grid(row=0, column=1)

        print(self.to_language)

    def IO(self):
        input_label = tk.Label(self.input_frame, text='Enter Text:')
        input_label.grid(row=0, column=0)

        self.input_field = tk.Text(self.input_frame, background=TEXT_FIELD,
                                   takefocus=True, font=('Leelawadee UI', 10),
                                   cursor='cross', selectbackground='#B2FF59')

        self.input_field.grid(row=1, column=0)

        translation_label = tk.Label(self.input_frame, text='Translation:')
        translation_label.grid(row=0, column=1)

        self.translated_field = tk.Text(self.input_frame, background=TEXT_FIELD,
                                        takefocus=True, font=('Leelawadee UI', 10),
                                        cursor='cross', selectbackground='#B2FF59')

        self.translated_field.grid(row=1, column=1)

    def Button(self):
        translate_button = tk.Button(self.button_frame, text='Translate', command=self.translateIt)
        translate_button.grid(row=0, column=0)

    def translateIt(self):
        translator = Translator(from_lang=self.from_language, to_lang=self.to_language)
        translation = translator.translate(self.input_field.get("1.0", 'end-1c'))
        print(translation)


if __name__ == '__main__':
    app = App()

    app.mainloop()