Search code examples
pythontkintertkinter-layouttkinter-texttkinter-label

Tkinter Texboxes Becoming Transparent On A Button Click


from tkinter import Tk, scrolledtext, INSERT, Button, PhotoImage, Label, Text

root = Tk()
root.geometry('1000x550')
bg = PhotoImage(file='./assets/bg.png')
root.resizable(False, False)
root.title('Testing Classes')
window = Label(root, image=bg)
window.place(relheight=1, relwidth=1)

tokenBox = Text(window, bd=0, height=1, width=30)


def get_token():
    token = tokenBox.get('1.0', 'end-1c')
    print(token)
    return token


prefixBox = Text(window, bd=0, height=1, width=20)
prefixBox.place(x=400, y=100)
tokenBox.place(x=350, y=150)


def get_prefix():
    prefix = prefixBox.get('1.0', 'end-1c')
    print(prefix)
    return prefix


def codeInsert():
    prefixBox.place(x=400, y=100)
    tokenBox.place(x=350, y=150)
    code = f'''
    import discord
    from discord.ext import commands

    bot = commands.Bot(prefix={get_prefix()})

    bot.run({get_token()})
    '''
    codeBox = scrolledtext.ScrolledText(window, width=50, height=15, bg='Black', fg='Red')
    codeBox.insert(INSERT, code)
    codeBox.configure(state='disabled')
    codeBox.place(x=300, y=300)


submit = Button(window, command=codeInsert, text='Submit Everything', bd=0)
submit.place(x=425, y=250)
window.mainloop()  

When I click on the Submit button, it hides all the textboxes. The textboxes only comes back, when I click on them. I still see the cursor change, when I hover on them, but the Labels also get hidden, and they are never shows back. It's like they become transparent, and become opaque only when I click on them.


Solution

  • Try this:

    from tkinter import Tk, scrolledtext, INSERT, Button, PhotoImage, Label, Text
    
    root = Tk()
    root.geometry('1000x550')
    bg = PhotoImage(file='./assets/bg.png')
    root.resizable(False, False)
    root.title('Testing Classes')
    window = Label(root, image=bg)
    window.place(relheight=1, relwidth=1)
    
    tokenBox = Text(root, bd=0, height=1, width=30)
    
    
    def get_token():
        token = tokenBox.get('1.0', 'end-1c')
        print(token)
        return token
    
    
    prefixBox = Text(root, bd=0, height=1, width=20)
    prefixBox.place(x=400, y=100)
    tokenBox.place(x=350, y=150)
    
    
    def get_prefix():
        prefix = prefixBox.get('1.0', 'end-1c')
        print(prefix)
        return prefix
    
    
    def codeInsert():
        # Add these lines here if you want to remove the button/entries.
        #tokenBox.place_forget()
        #prefixBox.place_forget()
        #submit.place_forget()
    
        code = f'''
        import discord
        from discord.ext import commands
    
        bot = commands.Bot(prefix={get_prefix()})
    
        bot.run({get_token()})
        '''
        codeBox = scrolledtext.ScrolledText(root, width=50, height=15, bg='Black', fg='Red')
        codeBox.insert(INSERT, code)
        codeBox.configure(state='disabled')
        codeBox.place(x=300, y=300)
    
    
    submit = Button(root, command=codeInsert, text='Submit Everything', bd=0)
    submit.place(x=425, y=250)
    window.mainloop()
    

    The problem is that you had the label (named window) as the master for the other widgets. You should never have a tk.Label as a parent for anything. When I changed all their parents to root it worked.