#๐Ÿ”’ Buttons are not set pixel length tkinter

113 messages ยท Page 1 of 1 (latest)

granite raptor
#

I have button widigets grided to a frame then the frame is packed to the screen. The buttons are added with an image of 1x1 pixle thewn use width=121 but when on screen they take up more space than the set width?

root.geometry("363x250")

pixel = PhotoImage(width=1, height=1)
buttonOptions = Button(buttonFrame, text='Settings',image=pixel, width=121,padx=0, height=30,compound = 'c', command=None)
buttonNextQuestion = Button(buttonFrame,image=pixel, height=30, width=121,padx=0,compound = 'c', text="Next Question", command=None)
buttonPreviousQuestion = Button(buttonFrame,image=pixel, height=30, width=121,padx=0,compound = 'c', text="Previous Question", command=None)
buttonCheckAnswer = Button(buttonFrame,image=pixel, height=30, width=121,padx=0,compound = 'c', text="CheckAnswer", command=lambda: checkAnswerFunction())

buttonOptions.grid(row=0, column=0)
buttonCheckAnswer.grid(row=0, column=1)
buttonNextQuestion.grid(row=0, column=2)

displayedFrame = radioButtonFrame
buttonFrame.grid(row=0, column=0)
displayedFrame.grid(row=1, column=0)
visual galeBOT
#

@granite raptor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

granite raptor
#

the buttons should be 121 pixles wide. 363/3=121 meaning they should fit perfectly on the screen

haughty shuttle
granite raptor
#

its not hence why i have to put an image in the button

#

it is*

haughty shuttle
#

are you just trying to get them to fill their frame?

granite raptor
#

i need them to be a spesific leanth in width so they scan fill the screen properly

haughty shuttle
#

you can just sticky them in the grid

#

then they'll be whatever size the frame is

granite raptor
#

precicly 121 long to fill the 323 width screen

haughty shuttle
#

I wouldn't rely on fixed values in a UI like that

#

you can make them resize with the window and always stretch to 1/3rd

#

in theory, if you added a 4th button, you would want it to just as easily fit in

#

without having to resize everything

granite raptor
#

how would i do that?

haughty shuttle
#

1 sec

granite raptor
#

thanks

haughty shuttle
#
import tkinter as tk


root = tk.Tk()
frame = tk.Frame(root)
frame.pack(fill = 'both', expand=True)
for i in range(3):
    tk.Button(frame, text = f'Button {i+1}').grid(row=0, column=i, sticky='ew')
    frame.grid_columnconfigure(i, weight=1)

root.mainloop()
#

try running this

#

look what happens when the window expands

granite raptor
#

ill will try this out

granite raptor
haughty shuttle
#

the fill/expand was for my frame pack

#

pack and grid don't have the same options

granite raptor
#

ah

#

Is it possible with grid?

haughty shuttle
#

Can you share your full code?

granite raptor
#

sure

haughty shuttle
#

if you're adding your frame with grid, you should just be able to use sticky='ew'

#

this tells your widget to anchor itself to east/west (which is the left/right side)

#

If I get rid of sticky, this happens. The columns are the same size, but the buttons don't completely fill out their columns

granite raptor
visual galeBOT
haughty shuttle
#

yup, just doing what I had above works fine

#

you need sticky in the buttons, and the buttonFrame needs the grid_columnconfigure

#

also this can be simplified

#
-command=lambda: checkAnswerFunction())
+command=checkAnswerFunction)
#

you don't need a lambda for a command unless it has an argument

granite raptor
#

I though it will run the command when i run the script if theres no a lmabanda

haughty shuttle
#

Only if you include the ()

#

checkAnswerFunction and checkAnswerFunction() are very different things

granite raptor
#

Ah i see

#

dident relise thats how it worked

haughty shuttle
#

the command expects a "callable"

granite raptor
#

ive just aways put the function in there

haughty shuttle
#

checkAnswerFunction is the function object itself (which is callable)
checkAnswerFunction() is a call to the function, so it's basically trying to use the return value as a command

granite raptor
#

ok

granite raptor
# haughty shuttle `checkAnswerFunction` is the function object itself (which is callable) `checkAn...

im still getting an error after adding grid_columnconfigure and sticky in the buttons

Traceback (most recent call last):
  File "/home/samms/PycharmProjects/FlasCards(ComputerSeince Revision/.venv/Scripts/Main.py", line 123, in <module>
    buttonFrame.grid(row=0, column=0, fill = 'both', expand=True)
  File "/usr/lib/python3.12/tkinter/__init__.py", line 2569, in grid_configure
    self.tk.call(
_tkinter.TclError: bad option "-fill": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky
haughty shuttle
#

fill and expand cannot be used in grid

#

if you're using grid instead of pack, just sticky should be enough

granite raptor
#

Ill try

haughty shuttle
#

you might want to use pack for your frames though

#

it's usually a good idea for top level frames so they can properly expand in the window

granite raptor
#

ill uses pack

#

its just i may be adding imsages into the questions will pack still work

haughty shuttle
#

packing the frames doesn't change the contents

#

every frame has a "geometry manager"

granite raptor
#

alright

haughty shuttle
#

you can use either pack or grid

#

but you can't mix and match for the same frame

#

so if I pack something into a frame, I can't also grid something into that same frame

#

but frames are nested

#

so I can pack a frame into the window

#

and then grid the elements (buttons, etc) into that frame

granite raptor
#

ive packed the buttons in insted but only 1 button resized the other two dont

haughty shuttle
#

how did you do grid_columnconfigure?

granite raptor
#

without the for loop bit

#

does it need to be done for all of the buttons

haughty shuttle
#

it's done for the column

#

the for loop I had made sure all columns were set to weight 1

#
for i in range(3):
    buttonFrame.grid_columnconfigure(i, weight=1)
granite raptor
#

alright isee

haughty shuttle
#

you can just do this

#

you have 3 columns

granite raptor
#

yes its working now thanks

haughty shuttle
#

np!

#

layout management is definitely one of the more confusing things to wrap your head around in tkinter

granite raptor
#

ive tried putting the same thing in the raidio buttons but its not doing anything

#

width wise

haughty shuttle
#

what are you trying to do with them?

#

something like this?

granite raptor
#

yes that would work

haughty shuttle
#

I think it would like nicer if you left align the buttons, but then you end up with something like this

#

for this, I added lightgray to the radio button frame

granite raptor
#

but i want the text to strech with the screen

#

is that possibler

haughty shuttle
#

yes, it will just the same as the upper buttons

granite raptor
#

thats good

haughty shuttle
#

see if you can apply the same idea that you did for the button buttons to the radio buttons and frame

granite raptor
#

Theres 3 buttons so im using the same for loop

for i in range(3):
    buttonFrame.grid_columnconfigure(i, weight=1)
    radioButtonFrame.grid_rowconfigure(i, weight=1)
    radioButtonFrame.grid_columnconfigure(i, weight=1)
buttonFrame.pack(fill = 'both', expand=True)
displayedFrame.pack(fill = 'both', expand=True)
visual galeBOT
#

Hey @granite raptor!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
haughty shuttle
#

and rowconfigure is just the vertical space, which you likely don't need to expand as the window gets taller

granite raptor
#

so just radioButtonFrame.grid_columnconfigure(0, weight=1)

haughty shuttle
#

yes

granite raptor
#

and sticky center?

haughty shuttle
#

it also needs to be packed with expand/fill flags

#
buttonFrame.pack(fill='both', expand=True)
displayedFrame.pack(fill='both', expand=True)
haughty shuttle
#

it won't stick to anything

#

but also if you're using pack, you don't have to worry about sticky

granite raptor
#

every thing is working now

visual galeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.