Home > Software engineering >  Python Tkinter Status Bar / Toolbar Toggle On/Off Example
Python Tkinter Status Bar / Toolbar Toggle On/Off Example

Time:08-28

It's nearly 4 in the morning and I've been pulling my hair out with this to even get this far with it. I've managed to make my toggle_status_bar on/off function work correctly. But now my trouble is simply presetting the statusbar according to the user's pre-defined preference (that I'll load up from a file later, haven't written this part yet). This code works great if "users_default_status_bar_setting" is preset to 0 (OFF). But, if I preset it to 1 (ON), the menu checkbutton is checked correctly but the status bar doesn't show up.

import tkinter as tk
import tkinter.ttk as ttk

# Root
root = tk.Tk()
root.minsize(720, 480)
root.rowconfigure(1, weight=1)
root.columnconfigure(1, weight=1)

# Variables
statusbar_is_on = tk.IntVar()

# Menu
menu = tk.Menu(root)


# Toggle status bar on/off
def toggle_status_bar():
    global status_bar
    if statusbar_is_on.get() == 1:
        # ON
        status_bar = ttk.Frame(root)
        status_bar.grid(row=3, column=1, sticky="SWE")

        test_label = ttk.Label(status_bar, text="STATUS BAR")
        test_label.grid(row=1, column=1, sticky="WE")
    else:
        # OFF
        status_bar.destroy()


# View Menu
view_menu = tk.Menu(menu, tearoff=0)
view_menu.add_checkbutton(label="Status Bar", onvalue=1, offvalue=0, variable=statusbar_is_on, command=toggle_status_bar)
menu.add_cascade(label="View", menu=view_menu)

# Menu End
root.config(menu=menu)

# Text
text_frame = ttk.Frame(root)
text_frame.grid(row=1, column=1, sticky="NEWS")
text = tk.Text(text_frame)
text.grid(row=1, column=1, sticky="NEWS")
text_vertical_scrollbar = ttk.Scrollbar(root, orient="vertical", command=text.yview)
text_vertical_scrollbar.grid(row=1, column=2, sticky="NS")
text.configure(yscrollcommand=text_vertical_scrollbar.set)
text_frame.rowconfigure(1, weight=1)
text_frame.columnconfigure(1, weight=1)

# User Default Settings
# TODO: Load up user .ini file
users_default_status_bar_setting = 1    ### PROBLEM HERE ###
if users_default_status_bar_setting == 1:
    statusbar_is_on.set(1)
    print("User wants the status bar ON, when the program first starts up")
else:
    statusbar_is_on.set(0)
    print("User wants the status bar OFF, when the program first starts up")

root.mainloop()

CodePudding user response:

You are not calling toggle_status_bar when you are starting up the program.

Just add it here:

users_default_status_bar_setting = 1
if users_default_status_bar_setting == 1:
    statusbar_is_on.set(1)
    print("User wants the status bar ON, when the program first starts up")
else:
    statusbar_is_on.set(0)
    print("User wants the status bar OFF, when the program first starts up")
# Added missing call.
toggle_status_bar()

CodePudding user response:

Easier way to do this in Python 3.8 using Walrus.

#users_default_status_bar_setting = 1    ### PROBLEM HERE ###
if [users_default_status_bar_setting := 1] != 1:
    statusbar_is_on.set(1)
    print("User wants the status bar ON, when the program first starts up")
else:
    statusbar_is_on.set(0)
    print("User wants the status bar OFF, when the program first starts up")
toggle_status_bar()
  • Related