Как задать размер окна в tkinter
Перейти к содержимому

Как задать размер окна в tkinter

How to Create Full Screen Window in Tkinter?

There are two ways to create a full screen window in tkinter using standard python library for creating GUI applications.

Method 1: Using attributes() function

Syntax:

We will set the parameter ‘-fullscreen’ of attributes() to True for setting size of our window to fullscreen and to False otherwise.

Approach:

  • Importing tkinter package
  • Creating a tkinter window with name window
  • Setting the window attribute fullscreen as True
  • Giving title to the window, here ‘Geeks For Geeks’
  • Creating a label with text ‘Hello Tkinter’ (just for display to the user here)
  • Placing the label widget using pack()
  • Closing the endless loop of windows by calling mainloop()

Disadvantage:

We get an output tkinter WINDOW with no toolbar. This disadvantage is covered by the next method.

Program

Python3

Output:

Method 2: Using geometry() function

We get an output tkinter window with the toolbar above along with the title of window.

Syntax:

We can set the parameter of geometry() same as the width*height of the screen of our original window to get our full screen tkinter window without making the toolbar invisible. We can get the width and height of our desktop screen by using winfo_screenwidth() and winfo_screenheight() functions respectively.

How to set a tkinter window to a constant size

I’m programming a little game with tkinter and briefly, I’m stuck.

I have a kind od starting menu, in which are two buttons and one label.

If I just create the frame everything is fine, it has the size 500×500 pixels

I want the background not to change when I create the buttons and the labe, but it adapts the size whatever I do. Here is my code:

I’ve searched around on stackoverflow and didn’t get anything useful! I’ve found just one question a bit similar to mine but the answer didn’t work. I tried this:

(1) mw.resizable(width=False, height=False)

I can’t imagine what is the problem, I’m really desperate.

6 Answers 6

You turn off pack_propagate by setting pack_propagate(0)

Turning off pack_propagate here basically says don’t let the widgets inside the frame control it’s size. So you’ve set it’s width and height to be 500. Turning off propagate stills allows it to be this size without the widgets changing the size of the frame to fill their respective width / heights which is what would happen normally

To turn off resizing the root window, you can set root.resizable(0, 0) , where resizing is allowed in the x and y directions respectively.

To set a maxsize to window, as noted in the other answer you can set the maxsize attribute or minsize although you could just set the geometry of the root window and then turn off resizing. A bit more flexible imo.

Whenever you set grid or pack on a widget it will return None . So, if you want to be able to keep a reference to the widget object you shouldn’t be setting a variabe to a widget where you’re calling grid or pack on it. You should instead set the variable to be the widget Widget(master, . ) and then call pack or grid on the widget instead.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *