简体   繁体   中英

Tkinter ttk Combobox Default Value

I'm building a Tkinter application and I came across an issue with setting a default value to a combobox. I managed to fix the problem, but I am curious to know why it worked and I would like to know if there is a better way to do it.

I have a tk.Toplevel() window pop up with a combobox using the fowling code:

class add_equation():

    def __init__(self):

        self.add_window = tk.Toplevel()
        self.add_window.title("Add Equation Set")
        self.add_window.resizable(width=False, height=False)

        self.name_entry_var = tk.StringVar()
        self.name_entry = ttk.Entry(self.add_window, textvariable=self.name_entry_var, width=30)
        self.name_entry.grid(row=1, columnspan=2, stick="w")

        self.equation_type_var = tk.StringVar()
        self.equation_type = ttk.Combobox(self.add_window, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28, postcommand =lambda: self.add_window.update())
        self.equation_type.current(0)
        self.equation_type.grid(row=2, columnspan=2, sticky="w")

        self.add_window.update()

The class add_quation() is called in the following bit of code:

import tkinter as tk
from tkinter import ttk

class Solver_App(tk.Tk, ttk.Frame):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        list_frame = ttk.Frame(self, height=50)
        list_frame.pack(side="top", fill="y", expand=True, anchor="w")

        # Button that will call the class add_equation that creates a new window.
        add_button = ttk.Button(list_frame, text="Add Equation Set", command=add_equation)
        add_button.pack(side="top", expand=False, fill="x", padx=10, pady=5, anchor="n")

app = Solver_App()
app.mainloop()

Looking back at the add_equation() class, if you remove self.equation_type.current(0) or postcommand =lambda: self.add_window.update() , the default value will no longer show, but with both, it works just fine. Why is it working like this instead of only having self.equation_type.current(0) ?

I've tried to find a more elegant way of doing this, and I found something related over here , but I had no luck implementing that method and I assume calling add_equation() from a button command may have something to do with that.

*I'm using Python 3.4 on Mac OS X Yosemite.

I think this is probably because you're creating a window by calling the add_equation constructor, and that window is immediately garbage collected (or atleast the python handle to it is) so never gets properly refreshed.

I'd rewrite it as something like this:

class Equation_Window(tk.Toplevel):

    def __init__(self):

        tk.Toplevel.__init__(self)

        self.title("Add Equation Set")
        self.resizable(width=False, height=False)

        self.name_entry_var = tk.StringVar()
        self.name_entry = ttk.Entry(self, textvariable=self.name_entry_var, width=30)
        self.name_entry.grid(row=1, columnspan=2, stick="w")

        self.equation_type_var = tk.StringVar()
        self.equation_type = ttk.Combobox(self, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28)
        self.equation_type.current(0)
        self.equation_type.grid(row=2, columnspan=2, sticky="w")

def add_equation():
    w = Equation_Window()
    w.wait_window()

(everything else remains the same)

I've changed the your add_equation class to something derived from a tk.Toplevel (and renamed it), which I think makes more sense. I've then made add_equation a function, and called wait_window (which acts like mainloop but just for one window). wait_window will keep w alive until the window is closed, and so everything gets refreshed properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM