Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

Python • Re: Simple GUI app not displaying combo boxes & buttons when Auto Run.

$
0
0
When I run your code I get the following error

Code:

Exception in Tkinter callbackTraceback (most recent call last):  File "/usr/lib/python3.13/tkinter/__init__.py", line 2071, in __call__    return self.func(*args)           ~~~~~~~~~^^^^^^^  File "/home/andrew/./test.py", line 57, in create_window    combo.set(read_line(4))              ~~~~~~~~~^^^  File "/home/andrew/./test.py", line 28, in read_line    with open("your_file.txt", "r") as f:         ~~~~^^^^^^^^^^^^^^^^^^^^^^FileNotFoundError: [Errno 2] No such file or directory: 'your_file.txt'
This stops the "set delay" spinner and "save" and "exit" buttons from showing. If I create the file it works fine.

I suspect that when you "auto run" your code the program is not finding "your_file.txt" and is causing this error. Perhaps put the full path to "your_file.txt" and try that.

To make your code more robust you will need to handle these exceptions. You can then pop up error messages in Tkinter with the messages.

Something like this:

Code:

#!/usr/bin/env python3# Import moduleimport osfrom tkinter import *from tkinter import messagebox, ttkimport tkinter as tksd_card_path = "/home/user/Python/"file_name = "data.txt"file_path = os.path.join(sd_card_path, file_name)# ----------- New window -------------------------def create_window():    def write_line(line_number, text):        try:            with open(file_path, "r") as f:                lines = f.readlines()            if 0 < line_number <= len(lines):                lines[line_number - 1] = text + "\n"            else:                return "Line number out of range"            with open("your_file.txt", "w") as f:                f.writelines(lines)                messagebox.showinfo( title="Data Update", message="Saved value", parent=new_window )        except Exception as e:            messagebox.showerror("Error", str(e))    def read_line(line_number):        try:            with open("your_file.txt", "r") as f:                lines = f.readlines()            if 0 < line_number <= len(lines):                return lines[line_number - 1].strip()  # Line numbers start at 1            else:                return "Line number out of range"        except Exception as e:            messagebox.showerror("Error", str(e))            return ""    def get_value():        delay = (selected_value.get())        write_line(4, "var4 = " + delay)    new_window = Toplevel()    new_window.title("Settings")    new_window.geometry( "400x400" )    label = Label(new_window, text="Settings")    label.pack(padx=20, pady=20)    button1 = Button( new_window , text = "Exit settings", width=35 ).pack()    button2 = Button( new_window , text = "Spare", width=35).pack()    button3 = Button( new_window , text = "Exit App", width=35).pack()    label1 = Label( new_window , text = "Set Delay " )    label1.place(x=25, y=170)    selected_value = tk.StringVar()    combo = ttk.Combobox(new_window, textvariable=selected_value)    combo['values']=("300","250","225","200","175","150","140","130","125")    combo.set(read_line(4))    combo.bind("<<ComboboxSelected>>") #("<<ComboboxSelected>>", selection_changed)    combo.place(x=110, y=170)    button4 = Button( new_window , text = "Save", command=get_value )    button4.place(x=100, y=260)    button5 = Button( new_window , text = "Exit", command=new_window.destroy )    button5.place(x=160, y=260)# --------------- Main window  -------------------------------root = Tk()root.title("  Main Window  ")root.geometry( "400x500" ) #(HxV)button = Button( root , text = "Settings" , command = create_window )button.place(x=150, y=200)# Execute tkinterroot.mainloop()

Statistics: Posted by AndyD — Sat Dec 06, 2025 8:53 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles