0

How can I make a tkinter Button completely fill the entire gui temporarily, and then after hitting said button, return to previous state.

I tried setting the button to my "highest level" frame and using the expand and fill config settings, this made the Button pretty big, but ultimately it only filled the bottom 1/3 of my gui.

... other instantiations...

#Initialization of button in gui as whole
toggleBacklightButton = Button(patternOptionFrame,text="Screen Light",
                               font=('calibri',(10)),relief="raised",
                               command=toggleBacklight)
toggleBacklightButton.grid(row=0,column=3)

... other code...

#Function that the button press calls.
def toggleBacklight():
    global backlight_toggle
    backlight_toggle = not backlight_toggle
    if backlight_toggle is True:
        # Button should be as it was when instantiated AND back light
        # is on / all other ~20 widgets are also where they belong.
        os.system(
            "sudo sh -c 'echo \"0\" > /sys/class/backlight/rpi_backlight/bl_power'")
    else:
        # Button should fill entire screen for ease of access when
        # screen is black / all other ~20 widgets are hidden.
        os.system(
            "sudo sh -c 'echo \"1\" > /sys/class/backlight/rpi_backlight/bl_power'")

... other functions...

The button does toggle my touch screen display, however, I don't know how to make it take up the whole screen when the screen back light is off.

martineau
  • 119,623
  • 25
  • 170
  • 301
codeNoob
  • 15
  • 3
  • You might be able to create multiple "pages" (`tk.Frames`) and use `frame.tkraise()` to control which one is on top and covering-up all the others. See this [answer](https://stackoverflow.com/a/7557028/355230) to another `tkinter` question for an example. – martineau Jan 15 '19 at 05:26

2 Answers2

1

Tkinter does not normally allow widgets to overlap at all - making your button bigger just pushes other widgets away, it will never actually cover them. In the extremely rare case where you do want overlap, only the .place() geometry manager can do it. Make your button a direct child of the window itself, and do:

toggleBacklightButton.place(x=0, y=0, relwidth=1.0, relheight=1.0)

to make it take over the window, then:

toggleBacklightButton.place_forget()

to get rid of it.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
  • If I make the frame for my button "root" aka tk() aka the window as you stated, my button will not be in the correct frame. This method did fill the entire screen which makes it seem I am half way there, however the fact that this method is dependent on my button being in the root frame rather than the "patternOptionFrame" ruins my layout. Is it possible to change what frame a button is in after it is instantiated or must I recreate the button with each toggle? My code structure would be clearer if I could do the former. – codeNoob Jan 15 '19 at 05:53
  • Your suggestion worked out, however I am still concerned with using the place_forget() method. Either I should have been using this as a convention, or I hope there is another way to do this. I am working on this project as a means to learn good code practice, and I don't like that I now have two different styles of "toggling" buttons – codeNoob Jan 15 '19 at 06:02
  • I believe you can overlap widgets using `grid` too when you give them the same row and column (or let one widget span multiple cells and grid the other widget in one of those cells). – fhdrsdg Jan 15 '19 at 08:26
0

If you want to use overlapping widgets then build everything inside of a frame and place you button on the same grid location of the frame.

Something like this:

import tkinter as tk


root = tk.Tk()

def action():
    btn.destroy()

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

frame = tk.Frame(root)
frame.grid(row=0, column=0, sticky="nsew")
tk.Label(frame, text="some random label").pack()

btn = tk.Button(root, text="Some big button", command=action)
btn.grid(row=0, column=0, sticky="nsew")

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79