Lzypenguin
Lzypenguin

Reputation: 955

Is there a more efficient way to update labels on tkinter?

Here is my code. It works how I want it to but I have always been told it is poor coding practice to use global variables and that they can cause problems, although I cannot figure out how to get the labels to change without using them. Any help is appreciated.

import tkinter as tk
from tkinter import filedialog, Text
import os

status = 'Start'
startStopBG = 'light green'

def main():
    root = configure_screen()
    root.mainloop()


def configure_screen():
    root = tk.Tk()
    root.title('APP')
    root.config(bg='snow3')
    root.minsize(700, 700)

    browse_button = tk.Button(root, text='Browse', width='10', command=browse)
    browse_button.place(x=605, y=10)

    global text
    text = tk.Text(root, height=1.3, width=73)
    text.insert(tk.END, 'Enter Path to Storage HERE')
    text.place(x=10, y=13)

    global start_stop
    start_stop = tk.Button(root, height=1, width=12, text=status, bg=startStopBG,
                           font=('Helvetica', '40'), command=start_scanning)
    start_stop.pack(pady=50)
    return root


def browse():
    path = filedialog.askdirectory(initialdir='/', title='Select where you want to save your file')
    text.delete('1.0', tk.END)
    text.insert(tk.END, path)

def start_scanning():
    global status
    global startStopBG
    global start_stop
    if status == 'Start':
        status = 'Stop'
        startStopBG = 'red'
    else:
        status = 'Start'
        startStopBG = 'light green'
    start_stop.config(text=status, bg=startStopBG)


if __name__ == '__main__':
    main()

Upvotes: 1

Views: 107

Answers (2)

Aditya
Aditya

Reputation: 1329

As I understood you want to change label

Try This:

import tkinter as tk

def main():

    def change_label_text():
        mylabel.config(text="yeee My Text has Been changed")

    def change_button_text():
        mybutton.config(text="yee Button text has been changed")

    root = tk.Tk()
    root.title('Change Label')
    root.config(bg='snow3')
    root.geometry('400x300')

    mybutton = tk.Button(root, text='Press Me To Change Button Text', command=change_button_text)
    mybutton.pack(side='top')

    mylabel = tk.Label(root, text='Press The Button Above To Change My text')
    mylabel.pack(side='bottom')

    mybutton2 = tk.Button(root, text="Press me", command=change_label_text)
    mybutton2.pack(side='bottom')
    root.mainloop()

main()

By making functions inside the mainloop you dont need to do global stuff and all

Upvotes: -1

Flavio Moraes
Flavio Moraes

Reputation: 1351

First of all you can use a class to your main window and instead of global variables you use class variables. Second I would recommend you to use tkinter variables to store important data from widget since the path and the status. For example, if you use text=tk.StringVar() you can set or get the value from text with text.set('value') or text.get(). Tkinter variables are object and if you define an object in your main you can access it as a global variable inside functions without the need of using global. However, in your code, to use text as a StringVar you should change the Text widget for an Entry widget, which is more appropriated since path is a single entry value and not a text. The same way you can change your start_stop button to a Checkutton and it will make the color change unnecessary since you can define colors for background and selectcolor.

The code bellow includes all changes I suggest here:

import tkinter as tk from tkinter import filedialog, Text import os

class Main(tk.Tk):
    def __init__(self):
        super(Main, self).__init__()
        self.title('APP')
        self.config(bg='snow3')
        self.minsize(700, 700)

        self.status = tk.IntVar()
        self.text = tk.StringVar(self, value='Enter Path to Storage HERE')

        browse_button = tk.Button(self, text='Browse', width='10', 
                                  command=self.browse)
        browse_button.place(x=605, y=10)

        tk.Entry(self, width=73, textvariable=self.text).place(x=10, y=13)

        self.start_stop = tk.Checkbutton(self, height=1, width=12, text="start", 
                                    font=('Helvetica', '40'), indicator=False, 
                                    bg='light green', selectcolor='red', 
                                    variable=self.status, command=self.start_scanning)
        self.start_stop.pack(pady=50)

    def browse(self):
        path = filedialog.askdirectory(initialdir='/', title='Select where you want to save your file')
        self.text.set(path)

    def start_scanning(self):
        if self.status.get():
            self.start_stop.config(text='stop')
        else:
            self.start_stop.config(text='start')


if __name__ == '__main__':
    Main().mainloop()

Upvotes: 2

Related Questions