Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

python - How do I resize buttons in pixels? (Tkinter)

I am making a Tic Tac Toe game in Python Tkinter but the buttons are rectangles, and I want them to all be 100x100 pixels in size. I tried using:

a1 = Button(root, text="", font="Helvetica 16 bold", command=a1, height=10, width=10)

(ignore the empty string and a1) and yet it does not resize it into a square. I have written most of the code and don't want to use frames to resize them. What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One simple method is to give the button an invisible 1x1 pixel image. When you do that, the width and height attributes are interpreted as pixels (or more accurately, screen units, which could also mean points, inches, or centimeters).

If you do this, you can set compound to the value "c" to mean that the button should show both text and the image, with both being centered on the window.

For example:

import Tkinter as tk
...
pixel = tk.PhotoImage(width=1, height=1)
button = tk.Button(root, text="", image=pixel, width=100, height=100, compound="c")
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...