tkinter button height and width
Asked Answered
W

3

11

I am trying to create a button and change the height and width using the code below but the actual button doesn't show physically. However if you hover over the area it is supposed to be and click it will open the new window. Any ideas?

import sys
from tkinter import *

#main menu
def mmWindow():
    mmWindow=Tk()
    mmWindow.geometry('600x600')


#first window   
mWindow= Tk()
mWindow.geometry('1920x1080+0+0')
mWindow.title('DMX512 Controller')

wtitle = Label (mWindow, text = "Pi DMX", fg = 'blue')
wtitle.place(x = 640, y = 100)

#main menu button
mmbutton = Button (mWindow, text = "Main Menu",command = mmWindow)
mmbutton.place( x=200, y = 200)
mmbutton.config(width=200, height=200)
Weathersby answered 15/11, 2013 at 15:34 Comment(4)
Actually, it does show physically. The problem is, since it is so huge, it is hard to distinguish from the rest of the window.Royalroyalist
school boy error!!! thanks, now my next problem i can change the width but the original error of the height still occurs, the actual physical size of the button doesnt change but the 'clicking' area does.......as you may have already guessed a noob to python.Weathersby
I'm a little confused as to what you are doing. Why do you want to make a button and then change its height/width one line after you place it on the window? What are you trying to do here? Are you trying to do anything special or are you just trying to get a button with a custom size placed on the window?Royalroyalist
just trying to create a larger button as the programme will eventually run on a 7" touchscreen, so i need a bigger button. So nothing special (i don't think anyway) just need a bigger surface area for somebody to touch. Hope that makes senseWeathersby
R
6

Regarding your initial question: the button does appear physically. The problem is, since it is so large, it is hard to distinguish from the rest of the window.

Now, you said that your ultimate goal is to change the size of a button. If so, then you are on the right track: you use the height and width options for this.

However, I would recommend that you make a few changes to your code:

  1. Don't make the button so huge. Even on a very big monitor, having a button be that size is way overkill.
  2. Don't make the window so huge. Nobody wants an application that takes up the entire screen.
  3. Use .grid instead of .place. Doing so will make it easier for you to place widgets where you want them.
  4. Set the height and width options when you make the button, not after it.
  5. There is no need to import sys here. Only import what you need.
  6. Don't import like this: from tkinter import *. Doing so dumps a whole bunch of names in the global namespace that can easily be overwritten.

Here is my version of your script:

import tkinter as tk

def mmWindow():
    mmWindow = tk.Tk()
    mmWindow.geometry('600x600')

mWindow = tk.Tk()
# You can set any size you want
mWindow.geometry('500x500+0+0')
mWindow.title('DMX512 Controller')

wtitle = tk.Label(mWindow, text="Pi DMX", fg='blue')
wtitle.grid(row=0, column=1)

# You can set any height and width you want
mmbutton = tk.Button(mWindow, height=5, width=20, text="Main Menu", command=mmWindow)
mmbutton.grid(row=1, column=1)

mWindow.mainloop()
Royalroyalist answered 15/11, 2013 at 17:2 Comment(6)
thanks for this, I've just copied your code and ran it, i then tried changing the height and it still doesn't change the size of the button as in the outline stays the same size, it does change the width but no the height.......Weathersby
@Weathersby - Huh. Well, I'm afraid I don't know why that is. The code I gave works on a normal OS (Windows, Mac, Linux). However, you did say "touchscreen", which would imply a tablet of some sort. I don't have a tablet with me so I can't troubleshoot this.Royalroyalist
perhaps it my set up, i'm not currently running it on a touchscreen, just my mac running IDLE python version 3.3.2 and tk version 8.5.14, i did think it was bizarre that the width changed and the height didn't :-sWeathersby
@Weathersby - Well, there is one more thing we can try. I heard once that IDLE has a hard time running Tkinter scripts for some reason. Instead, run the script directly from the command line a few times with different heights and see if that has an effect.Royalroyalist
I have just tried executing my code from terminal using the following python /Users/Josh/Documents/Uni/Year3/Diss/Python/DMX512.py and get the following error message Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'python' is not defined any ideas?Weathersby
@Weathersby - Try replacing "python" with the full path to the python executable. Some systems do not have the "python" alias installed.Royalroyalist
F
1
import sys
from tkinter import *

def update_window_size():
    mmWindow.geometry('600x600')

mmWindow  = Tk()
mmWindow .geometry('1920x1080+0+0')
mmWindow .title('DMX512 Controller')

wtitle = Label(mmWindow, text="Pi DMX", fg='blue')
wtitle.place(relx=0.33, rely=0.0925925)

mmbutton = Button(mmWindow, text="Main Menu", command=update_window_size)
mmbutton.place(relw=0.104167, relh=0.185185, relx=0.104167, rely=0.185185)

mmWindow.mainloop()

I know this is late, but just want to add my method of how to solve the issue of how to make the button size change. I believe using place with relw and relh will be a better way to go. relw and relh & relx and rely will be fraction of the height and width of the parent widget. Therefore you do not need to worry about adjusting the size of both the wtitle and mmbutton.

If you want to change it's width and height from place then just put the code below on button command.

def update_button_size():
    mmbutton.place(width=20, height=20)

mmbutton = Button(mmWindow, text="Main Menu", command=update_button_size)
mmbutton.place(width=400, height=400, relx=0.104167, rely=0.185185)

If you want to change it's width and height from config then use code below.

def update_button_size():
    mmbutton.config(width=20, height=20)

mmbutton = Button(mmWindow, text="Main Menu", command=update_button_size)
mmbutton.place(relx=0.104167, rely=0.185185)
mmbutton.config(width=400, height=400)

From my understanding config width and height is different from place width and height.

Feoffee answered 11/8, 2021 at 11:20 Comment(1)
More precisely, the width option of a Button typically corresponds to a number of letters, not a length in pixels (like the width in .place()), see anzeljg.github.io/rin2/book2/2405/docs/tkinter/button.html for instance. Your answer works but as advocated in the other answer, I think it is generally easier to use .grid() (or .pack() ) because you don't have to worry about overlapping widgets. I only use .place() for very specific purposes such as putting a ttk.SizeGrip in the bottom right corner of the window.Parent
S
1

You could use padx and pady:

mmbutton = Button(mmWindow, text="Main Menu", padx=43, pady=20)

or just remove your codes for trying to change the size and add:

mmbutton.config(padx=43, pady=20)

But make sure that you use grid instead of place!

I hope that I could help you.

Stopcock answered 28/7, 2023 at 2:44 Comment(1)
This was just what I needed when trying to fit loads of buttons without them being truncated from my screen.Lakisha

© 2022 - 2024 — McMap. All rights reserved.