Home > Software design >  Change object in an function
Change object in an function

Time:06-11

I am in the process of coding some programm using Tkinter. The idea is to have a mainwindow which changes its content by pressing buttons. If the content changed I want to creat a "back" button (as known from many programms), which leads back to the mainwindow.

I thought about putting the window elements in functions, so that the "back" button will trigger the function to creat the mainwindow. However, if I for example press the "settings" button, I want the content of the mainwindow to disappear to make room for the new stuff. The problem is, that those elements I want to delete, or grid.forget() are in a different function and therefor local variables. Is there any way to alter them?

here some code example:

"""Import"""
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import datetime
import json as js
from PIL import Image, ImageTk

"""Global variables"""
ACTIVATED = False



"""Functions"""

def main_window():

    global canvas, image

    root.configure(bg="white", padx=50, pady=50)
    canvas.destroy()
    canvas = tk.Canvas(width=100, height=100, bg="white", highlightthickness=0)
    resized_image = image.resize((100, 100), Image.ANTIALIAS)
    image = ImageTk.PhotoImage(resized_image)
    logo = canvas.create_image((50, 50), image=image)
    canvas.grid(row=0, column=0, columnspan = 4)

    title = tk.Label(text="TEXT", bg="white", width=6, font=("Edwardian Script ITC", 50,))
    title.grid(row=0, column=5, columnspan = 6, sticky = tk.E)

    empty = tk.Label(text="", font=("Arial", 12, "bold"), bg="white", pady=10,)
    empty.grid(row=1, column=1)

    settings_button = tk.Button(text="Settings", command=settings)
    settings_button.grid(row=2, column=0)

    analyse_button = tk.Button(text="Analyse data", command=analyse)
    analyse_button.grid(row=2, column=1)


def settings():

    settings_button.grid.forget()
    analyse_button.grid.forget()
   
    back_button = tk.Button(text="back", command=main_window)
    back_button.grid(row=2, column=1)

    some new stuff

thanks for your kind advices.

CodePudding user response:

I would recommend wrapping your main window into a class instead of handling it through functions only. Create an instance of your class. You can access all elements you want to hide or show etc. through its attribute self.<element>.

I slightly modified your code to look like this:

"""Import"""
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import datetime
import json as js
from PIL import Image, ImageTk

class myClass():

    """Functions"""

    def __init__(self):
        self.ACTIVATED = False

        self.root.configure(bg="white", padx=50, pady=50)
        self.canvas = tk.Canvas(width=100, height=100, bg="white", highlightthickness=0)
        self.resized_image = image.resize((100, 100), Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized_image)
        self.logo = canvas.create_image((50, 50), image=image)
        self.canvas.grid(row=0, column=0, columnspan = 4)

        self.title = tk.Label(text="TEXT", bg="white", width=6, font=("Edwardian Script ITC", 50,))
        self.title.grid(row=0, column=5, columnspan = 6, sticky = tk.E)

        self.empty = tk.Label(text="", font=("Arial", 12, "bold"), bg="white", pady=10,)
        self.empty.grid(row=1, column=1)

        self.settings_button = tk.Button(text="Settings", command=settings)
        self.settings_button.grid(row=2, column=0)

        self.analyse_button = tk.Button(text="Analyse data", command=analyse)
        self.analyse_button.grid(row=2, column=1)


    def settings(self):

        self.settings_button.grid.forget()
        self.analyse_button.grid.forget()
   
        self.back_button = tk.Button(text="back", command=main_window)
        self.back_button.grid(row=2, column=1)

        some new stuff

myObject = myClass()

#call when needed
myObject.settings()

This article only somewhat relates to your issue, but it demonstrates the concept fairly well.

Edit: What Grismar said in his comment, i.e. change/rewrite the whole structure, is a very good point, and most definitely the right solution in the long term. I aim to keep your structure and work with that. I have to note though, that I fully agree with Grismars opinion!

  • Related