0

So I just learn tkinter and want to create a custom titlebar and change it colors base on login and signup page. I manage to change the color between login frame and signup frame. However I struggle changing the color of the tittle bar, I tried many functions like pack_forget(), lift(), loop frame.winfo_children() and destroy widget

the problem is I cannot manage to remove the frame of title when switching between login and signup page.

for convenient this is the source code : https://github.com/SeakMengs/Sign-in-gui-with-python-tkinter

class tittle():
    def __init__(self):

        global FrameBG, FontFG, BoxBG, SignupBG, SignupFG, SignupBOX, LoginBG , LoginFG, LoginBOX
        global inLogin, inSignup

        if inLogin == True:
            FrameBG, FontFG, BoxBG = LoginBG, LoginFG, LoginBOX
        if inSignup == True:
            print('change to purple')
            FrameBG, FontFG, BoxBG = SignupBG, SignupFG, SignupBOX

        self.root = root
        self.titlebar = Frame(root, background=FrameBG, bd=0)

    def titleBar(self):
        # ? Create my own titlebar
        global RunOnce

        root.overrideredirect(True)
        root.after(10, lambda: self.set_appwindow(root))
        # self.titlebar = Frame(root, background=FrameBG, bd=0)
        #*----------------------------------------------------------------------------------------------------------------------------------------
        # ? these functions copy from https://stackoverflow.com/questions/63217105/tkinter-overridedirect-minimizing-and-windows-task-bar-issues
        self.titlebar.bind('<Button-1>', self.SaveLastClickPos)
        self.titlebar.bind('<B1-Motion>', self.Dragging)
        self.titlebar.bind("<Map>", self.frameMapped) # This brings back the window
        #*----------------------------------------------------------------------------------------------------------------------------------------

        self.icon = Image.open('asset\\bank.png')
        self.icon = self.icon.resize((22, 22))
        self.icon = ImageTk.PhotoImage(self.icon)
        Label(self.titlebar, text='  Yato Bank', font=('Lexend', 16), bg=FrameBG, fg=FontFG, image=self.icon, compound=LEFT).pack(side=LEFT, pady=10, padx=10)

        # ? Open image using pil (for resize purpose)
        self.closeIcon = Image.open("asset\\close.png")
        self.closeIcon = self.closeIcon.resize((24, 24))
        self.closeIcon = ImageTk.PhotoImage(self.closeIcon)
        Button(self.titlebar, image=self.closeIcon, command=root.destroy, background=FrameBG, bd=0, activebackground=FrameBG).pack(side=RIGHT, pady=10, padx=10)

        self.maximizeIcon = Image.open("asset\\maximize.png")
        self.maximizeIcon = self.maximizeIcon.resize((24, 24))
        self.maximizeIcon = ImageTk.PhotoImage(self.maximizeIcon)
        Button(self.titlebar, image=self.maximizeIcon, command=self.fullScreen, background=FrameBG, bd=0, activebackground=FrameBG).pack(side=RIGHT, pady=10, padx=10)

        self.minimizeIcon = Image.open("asset\\minus.png")
        self.minimizeIcon = self.minimizeIcon.resize((24, 24))
        self.minimizeIcon = ImageTk.PhotoImage(self.minimizeIcon)
        Button(self.titlebar, image=self.minimizeIcon, command=self.minimizeGUI, background=FrameBG, bd=0, activebackground=FrameBG).pack(side=RIGHT, pady=10, padx=10)

        self.titlebar.pack(expand=0, fill=BOTH)

        RunOnce += 1

        if RunOnce > 1:
            self.titlebar.pack_forget()
            self.titlebar.pack(expand=0, fill=BOTH)
            self.titlebar.lift()
            pass
    def startBar(self):

        self.titlebar.pack_forget()
        tittle().titleBar()
        print("Destroyed")

These functions are for switching between signin and signup page and it call class tittle() and its functions


    #* switch frame
    def startLogin(self):

        global inLogin, inSignup
        inSignup = False
        inLogin = True

        # tittle().titlebar.destroy()
        # tittle().startBar()
        self.form.pack(expand=1)
    
    def changeToSignup(self):

        global inLogin, inSignup
        inSignup = True
        inLogin = False

        tittle().startBar()
        self.form.pack_forget()
        signupPage().startSignup()

    def startSignup(self):

        global inLogin, inSignup
        inSignup = True
        inLogin = False
        
        # tittle().titlebar.destroy()
        # tittle().startBar()
        self.signupFrame.pack(expand=1)

    def changeToLogin(self):

        global inLogin, inSignup
        inSignup = False
        inLogin = True

        tittle().startBar()
        self.signupFrame.pack_forget()
        loginPage().startLogin()


Yato
  • 25
  • 1
  • 3

1 Answers1

1

You have not clearly defined the variable name of the title bar. I thought it was self.titlebar. You have commented the line in which you defined the titlebar. And the second confusing point is where you are calling the pack_forget function. I thought that is in if condition in the titlebar() function. If so, you are directly packing the frame after unpacking the frame which makes no sense. First try to debug yourself, then post the question.

Pycoder
  • 92
  • 9
  • Basically I tried to debug for many hours and tried a lot of solutions already but after hours of trying I get overwhelmed and decide to ask for help and the code in my class is a mess because I'm trying to find a solution then after solving the problem I will remove the unnecessary parts! Anyway thanks for your suggestions – Yato Oct 11 '22 at 15:38
  • Remove the unnecessary parts and then post the code. Sometimes they are the lines causing the bugs – Pycoder Oct 12 '22 at 12:48
  • I forgot about this, i wrote a simpler code that explains what i need and a guy remind me to use configure and it solves the problem already here's the link to a new question i post ytd https://stackoverflow.com/questions/74034743/python-tkinter-how-to-change-the-color-of-frame1 – Yato Oct 12 '22 at 19:35