Home > Back-end >  Python General GUI concepts
Python General GUI concepts

Time:05-05

I am starting off with some Python (tkinter) GUI projects, but I feel like I am missing some fundamental concepts in the code organisation here.

For example, I have a section to define the GUI elements (entry fields, buttons etc) and a section to define the functions which will be called by the GUI elements. However I find myself in a catch 22 situation because if I define the GUI elements first then I get an error that I am referring to undefined functions and if I define the functions first (e.g. show the value of x in field y) I get an error that the GUI fields are not defined.

Lets say with the code below I want to update the values of 2 labels at the press of a button

#defining the GUI elements
labelhighvalue1d_ticker1 = Label(root, text=" Value 1 ")
labelhighvalue1d_ticker1.grid(row=1, column=6)

labelhighvalue_lt_ticker1 = Label(root, text=" Value 2 ")
labelhighvalue_lt_ticker1.grid(row=1, column=7)

placetrade3 = tk.Button(root, text='Get Data', command=updatelabelsvalue)
placetrade3.grid(row=3, column=5)


def calculatevalues():
    return value1, value2

calculatevalues()

def updatelabelsvalue(value1, value2):
    labelhighvalue1d_ticker1 = Label(root, text=value1)
    labelhighvalue_lt_ticker1 = Label(root, text=value2)
   

At the time of defining the button the function is not yet defined -> error.

If I define the fiction at the beginning then the labels are not yet defined -> error.

Any quick guidance or related suggested reading would be appreciated.

CodePudding user response:

If I define the fiction at the beginning then the labels are not yet defined -> error.

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine, not a compiler, the statement under function updatelabelsvalue will not be checked until this function called, so it won't get error if you define the function at the beginning.

Your code not work, so revised here.

from tkinter import *

def updatelabelsvalue(value1, value2):
    labelhighvalue1d_ticker1.configure(text=" Value 3 ")
    labelhighvalue_lt_ticker1.configure(text=" Value 4 ")

root = Tk()

#defining the GUI elements
labelhighvalue1d_ticker1 = Label(root, text=" Value 1 ")
labelhighvalue1d_ticker1.grid(row=0, column=0)

labelhighvalue_lt_ticker1 = Label(root, text=" Value 2 ")
labelhighvalue_lt_ticker1.grid(row=1, column=0)

placetrade3 = Button(root, text='Get Data', command=lambda value1=" Value 3 ", value2=" Value 4 ":updatelabelsvalue(value1, value2))
placetrade3.grid(row=2, column=0)

root.mainloop()

CodePudding user response:

What you are missing is the understanding of event driven programming. Basically, GUI programming is based on events. When you run your sample code, all the code gets executed in a single stretch and it pauses when it reaches mainloop() and that is when the events start to process. For a GUI to stay alive, it needs to be able to process events.

What you should be doing is, create a button (just a way to trigger an event, when a button is clicked) and then run this function on the button click. So in this way you will be able to create an event that will fetch the return values and update the labels, after the widgets are created (the code has hit mainloop and started processing events).

#defining the GUI elements
labelhighvalue1d_ticker1 = Label(root, text=" Value 1 ")
labelhighvalue1d_ticker1.grid(row=1, column=6)

labelhighvalue_lt_ticker1 = Label(root, text=" Value 2 ")
labelhighvalue_lt_ticker1.grid(row=1, column=7)

def calculatevalues():
    return value1, value2

def updatelabelsvalue(): # Removed the parameters here
    val1,val2 = calculatevalues() # Run the function and get the return values and unpack into variables
    
    # Configure the text of the labels with the new text, rather than creating new labels
    labelhighvalue1d_ticker.config(text=val1) 
    labelhighvalue_lt_ticker1.config(text=val2)

placetrade3 = tk.Button(root, text='Get Data', command=updatelabelsvalue)
placetrade3.grid(row=3, column=5)

So now whenever the updatelabelsvalue is run, you will not get any errors as the widget is already created. Plus, I like to define ALL my functions between root and creation of GUI elements

CodePudding user response:

for me it is not so easy to find a good answer for your problem because you're question is very general. I recommend you for the beginning to start with PySimpleGui instead of tkinter. It is very easy and you will find a good documentation. That is how I started. Then after a while if necessary you can use tkinter or better PyQt.

But back to your question: The first step is to create the layout of your GUI. Then you define your events: for instance if button x is pressed, then...

  • Related