Python Forum
[Tkinter] class to replace areas of the screen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] class to replace areas of the screen
#1
Hi! I want to create a class with two parameters: the old area & the new area.
Instruction: destroy the old area and replace it with the new one.

What am I wrong in reasoning?

AttributeError: '_tkinter.tkapp' object has no attribute 'switch'

Windows 10, Python 3.9

import tkinter as tk

class Switch():
    def __init__(self, old_top, new_top):
        self.old_top=old_top
        self.new_top=new_top
    def switch(self):
        self.old_top.destroy()
        self.new_top=self.new_top(root)

class Calendario():    
    def __init__(self, master):

        frameTop=tk.Frame(master, bg='red')
        frameTop.grid(row=1, column=0, sticky="wens")
        frameTop.grid_propagate()

        self.bottone=tk.Button(frameTop, bg='white', relief="flat")
        self.bottone.grid(row=0, column=0)
    
        self.label=tk.Label(frameTop, text='Calendario!', font=('MV Boli', 16), bg='white')
        self.label.grid(row=0, column=1, padx=35)

class Top():    
    def __init__(self, master):

        frameTop=tk.Frame(master, bg='violet')
        frameTop.grid(row=0, column=0, sticky="wens")
        frameTop.grid_propagate()

        self.bottone=tk.Button(frameTop, bg='white', relief="flat", command=lambda: master.switch(Top2 ,Calendario))
        self.bottone.grid(row=0, column=0)
    
        self.label=tk.Label(frameTop, text='Hello!', font=('MV Boli', 16), bg='white')
        self.label.grid(row=0, column=1, padx=35)


class Top2():
    def __init__(self, master):

        frameTop=tk.Frame(master, bg='yellow')
        frameTop.grid(row=1, column=0, sticky="wens")
        frameTop.grid_propagate()

        self.bottone=tk.Button(frameTop, bg='white', relief="flat")
        self.bottone.grid(row=0, column=0)
       
        self.label=tk.Label(frameTop, text='Hello!', font=('MV Boli', 16), bg='white')
        self.label.grid(row=0, column=1, padx=35)

class Top3():
    def __init__(self, master):

        frameTop=tk.Frame(master, bg='pink')
        frameTop.grid(row=2, column=0, sticky="wens")
        frameTop.grid_propagate()

        self.bottone=tk.Button(frameTop, bg='white', relief="flat")
        self.bottone.grid(row=0, column=0)
        
        self.label=tk.Label(frameTop, text='Hello!', font=('MV Boli', 16), bg='white')
        self.label.grid(row=0, column=1, padx=35)

root=tk.Tk()
root.title('Hi!')
root.configure(bg='white')
Top1=Top(root)
Top2=Top2(root)
Top3=Top3(root)
root.mainloop()
Reply
#2
def switch is in the class Switch

class Switch is never used.
Reply
#3
(Apr-30-2021, 08:52 PM)Axel_Erfurt Wrote: def switch is in the class Switch

class Switch is never used.

Hi! Thanks for reply!

Why class Switch is never used?

So isn't Lambda enough?

command=lambda: master.switch(Top2 ,Calendario)
I tried to add instances but I always have the same mistake.

class Top():    
    def __init__(self, master):

        frameTop=tk.Frame(master, bg='violet')
        frameTop.grid(row=0, column=0, sticky="wens")
        frameTop.grid_propagate()

        old_top=Top  # new code
        old_top=Top2  # new code

        self.bottone=tk.Button(frameTop, bg='white', relief="flat", command=lambda: master.switch(Top2 ,Calendario))
        self.bottone.grid(row=0, column=0)
    
        self.label=tk.Label(frameTop, text='Hello!', font=('MV Boli', 16), bg='white')
        self.label.grid(row=0, column=1, padx=35)
Reply
#4
(May-01-2021, 10:18 AM)ANGOO Wrote: Why class Switch is never used?

You did not use it.

Look at this switch-between-two-frames-in-tkinter
Reply
#5
(May-01-2021, 11:24 AM)Axel_Erfurt Wrote:
(May-01-2021, 10:18 AM)ANGOO Wrote: Why class Switch is never used?

You did not use it.

Look at this switch-between-two-frames-in-tkinter

I thought I used the class Switc with ''command=lambda: master.switch(Top2 ,Calendario)''.

Thanks for the link. I read it carefully.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020