0

I'm new to python programming, and I have a bit of trouble with the program structure: When I make my GUI in the main python part then the code works:

import tkinter as tk

root = tk.Tk()
root.overrideredirect(True)
root.geometry("800x480")

def cb_Gebruiker():
    btnUser["text"]= "changed"

btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
btnUser.place(x=1,y=1,width="300",height="73")


root.mainloop()

When I make my GUI in a function, the btn variable is local, so this doesn't work

def MakeBtn():
    btnUser = tk.Button(root, text="User",command = cb_Gebruiker)
    btnUser.place(x=1,y=1,width="300",height="73")

def cb_Gebruiker():
    btnUser["text"]= "changed"

MakeBtn()


root.mainloop()

Now I have a program that's rather large and I want my GUI in a separate file, but then I can't access my GUI components... And I can't seem to be able to find a tutorial on how to structure a program (python has to many possibilities: script, module, object oriented,..)

How do I solve this?

  • 1
    ***"want my GUI in a separate file ... possibilities: `script`, `module`, `object oriented`"***: You need all of them, read [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) – stovfl Feb 02 '20 at 19:17

1 Answers1

0

You will need a lambda to delay the call to the command with a parameter.


def MakeBtn():
    btnUser = tk.Button(root, text="User", command = lambda: cb_Gebruiker(btnUser))
    btnUser.place(x=1, y=1, width="300", height="73")

def cb_Gebruiker(btnUser):
    btnUser["text"] = "changed"

MakeBtn()


root.mainloop()
quamrana
  • 37,849
  • 12
  • 53
  • 71