如何以像素为单位调整按钮的大小?(Tkinter) [英] How do I resize buttons in pixels? (Tkinter)

查看:47
本文介绍了如何以像素为单位调整按钮的大小?(Tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Python Tkinter 中制作 Tic Tac Toe 游戏,但按钮是矩形,我希望它们的大小均为 100x100 像素.我尝试使用:

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)

(忽略空字符串和 a1),但它不会将其调整为正方形.我已经编写了大部分代码,不想使用框架来调整它们的大小.我该怎么办?

(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?

推荐答案

我想要确保按钮具有特定大小(以像素为单位)的方法是将该按钮放置在框架内并确保框架设置为一个特定的大小,按钮被设置为填充该框架.

The way I would want to make sure a button is a specific size in pixels is by placing that button inside of a frame and making sure the frame is set to a specific size and the button is set to fill that frame.

这可以轻松转换为在您的程序中使用,并确保按钮设置为 100 x 100.

This can easily be converted to work in your program and it makes sure the buttons are set to exactly 100 x 100.

这是我所知道的以像素为单位确定按钮大小的唯一方法.可能还有其他选择.

This is the only way I am aware of to be sure of the size of a button in pixels. There may be other options.

首先我们需要 2 个列表.一个用来固定框架,一个用来固定按钮.这将使我们能够以一种简单的方式存储框架和按钮,以备日后使用.

First we will need 2 list. One to hold the frames and one to hold the buttons. This will allow us to store the frames and buttons in an easy way that we can work with later.

frames_list = []
btn_list = []

我不确定如何压缩你的 check_win() 函数,所以我只会对它做一个小改动,以便它可以与列表一起使用.我们需要将 a1a2a3 等的每个实例替换为按钮列表中对应的索引值,即稍后将使用 for 循环创建.

I am not sure off the top of my head on how to condense your check_win() function so I will only make a minor change to it so it will work with the lists. We need to replace each instance of a1, a2, a3, and so on with its counterpart in the buttons list with the index value that is going to be created with the for loop later.

def check_win():
    # Horizontal wins
    if btn_list[0]["text"] == btn_list[1]["text"] == btn_list[2]["text"] == "X" or btn_list[0]["text"] == btn_list[1]["text"] == btn_list[2]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[3]["text"] == btn_list[4]["text"] == btn_list[5]["text"] == "X" or btn_list[3]["text"] == btn_list[4]["text"] == btn_list[5]["text"] == "O":
        print("{} wins".format(btn_list[3]["text"]))
    elif btn_list[6]["text"] == btn_list[7]["text"] == btn_list[8]["text"] == "X" or btn_list[6]["text"] == btn_list[7]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[6]["text"]))

    # Vertical wins
    elif btn_list[0]["text"] == btn_list[3]["text"] == btn_list[6]["text"] == "X" or btn_list[0]["text"] == btn_list[3]["text"] == btn_list[6]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[1]["text"] == btn_list[4]["text"] == btn_list[7]["text"] == "X" or btn_list[1]["text"] == btn_list[4]["text"] == btn_list[7]["text"] == "O":
        print("{} wins".format(btn_list[1]["text"]))
    elif btn_list[2]["text"] == btn_list[5]["text"] == btn_list[8]["text"] == "X" or btn_list[2]["text"] == btn_list[5]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[2]["text"]))

    # Diagonal wins
    elif btn_list[0]["text"] == btn_list[4]["text"] == btn_list[8]["text"] == "X" or btn_list[0]["text"] == btn_list[4]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[2]["text"] == btn_list[4]["text"] == btn_list[6]["text"] == "X" or btn_list[2]["text"] == btn_list[4]["text"] == btn_list[6]["text"] == "O":
        print("{} wins".format(btn_list[2]["text"]))

    # If no one wins
    else:
        change_turn()

然后我们需要更改 process_turn() 函数以在按钮列表中包含每个按钮的索引值,因此像这样添加一个参数.

Then we need to change the process_turn() function to include the index value of each button in the buttons list so add an argument to it like this.

def process_turn(ndex): # index argument being sent by buttons
    btn_list[ndex].config(text=turn) # changed text at index of list.
    check_win()

最后,我们需要在命令中创建具有正确索引的所有按钮,我们可以使用 for 循环来完成此操作.好吧 2 for 循环.

Finally we need to create all the buttons with the correct index in the command and we can do this with a for loop. Well 2 for loops.

第一个循环将开始计算行数,第二个 for 循环将计算列数.这将创建我们的 3 x 3 网格.ndex 变量用于跟踪每个按钮需要在按钮列表上配置的索引.

The first loop will start the row count and the 2nd for loop will work the column count. This creates our 3 by 3 grid. The ndex variable is used to keep track of the index that each button needs to configure on the buttons list.

def create_frames_and_buttons():
    ndex = 0
    i = 0
    x = 0
    for i in range(3):
        for x in range(3):
            frames_list.append(Frame(root, width = 100, height = 100))
            frames_list[ndex].propagate(False)
            frames_list[ndex].grid(row = i, column = x, sticky = "nsew", padx = 2, pady = 2) # add any padding you would like to space out the frames/buttons
            btn_list.append(Button(frames_list[ndex], text="", font="Helvetica 16 bold",
                   command = lambda ndex=ndex: process_turn(ndex)))
            btn_list[ndex].pack(expand=True, fill=BOTH)
            x += 1
            ndex += 1
        i += 1
    root.resizable(width=False, height=False)

create_frames_and_buttons()

综合起来,您就有了这段代码,它具有您想要的精确 100x100 像素大小.

All put together you have this code that has the exact 100x100 pixel sizing you wanted.

看看下面的例子:

from tkinter import *

root = Tk()
frames_list = []
btn_list = []

turn = "X"
turnLabel = Label(root, text=turn, font="Helvetica 16 bold")
turnLabel.grid(row=3, columnspan=3)

def change_turn():
    global turn
    if turn == "O":
        turn = "X"
        turnLabel.config(text=turn)
    elif turn == "X":
        turn = "O"
        turnLabel.config(text=turn)

def check_win():
    # Horizontal wins
    if btn_list[0]["text"] == btn_list[1]["text"] == btn_list[2]["text"] == "X" or btn_list[0]["text"] == btn_list[1]["text"] == btn_list[2]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[3]["text"] == btn_list[4]["text"] == btn_list[5]["text"] == "X" or btn_list[3]["text"] == btn_list[4]["text"] == btn_list[5]["text"] == "O":
        print("{} wins".format(btn_list[3]["text"]))
    elif btn_list[6]["text"] == btn_list[7]["text"] == btn_list[8]["text"] == "X" or btn_list[6]["text"] == btn_list[7]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[6]["text"]))

    # Vertical wins
    elif btn_list[0]["text"] == btn_list[3]["text"] == btn_list[6]["text"] == "X" or btn_list[0]["text"] == btn_list[3]["text"] == btn_list[6]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[1]["text"] == btn_list[4]["text"] == btn_list[7]["text"] == "X" or btn_list[1]["text"] == btn_list[4]["text"] == btn_list[7]["text"] == "O":
        print("{} wins".format(btn_list[1]["text"]))
    elif btn_list[2]["text"] == btn_list[5]["text"] == btn_list[8]["text"] == "X" or btn_list[2]["text"] == btn_list[5]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[2]["text"]))

    # Diagonal wins
    elif btn_list[0]["text"] == btn_list[4]["text"] == btn_list[8]["text"] == "X" or btn_list[0]["text"] == btn_list[4]["text"] == btn_list[8]["text"] == "O":
        print("{} wins".format(btn_list[0]["text"]))
    elif btn_list[2]["text"] == btn_list[4]["text"] == btn_list[6]["text"] == "X" or btn_list[2]["text"] == btn_list[4]["text"] == btn_list[6]["text"] == "O":
        print("{} wins".format(btn_list[2]["text"]))

    # If no one wins
    else:
        change_turn()

def process_turn(ndex):
    btn_list[ndex].config(text=turn)
    check_win()

def create_frames_and_buttons():
    ndex = 0
    i = 0
    x = 0
    for i in range(3):
        for x in range(3):
            frames_list.append(Frame(root, width = 100, height = 100))
            frames_list[ndex].propagate(False)
            frames_list[ndex].grid(row = i, column = x, sticky = "nsew", padx = 2, pady = 2)
            btn_list.append(Button(frames_list[ndex], text="", font="Helvetica 16 bold",
                   command = lambda ndex=ndex: process_turn(ndex)))
            btn_list[ndex].pack(expand=True, fill=BOTH)
            x += 1
            ndex += 1
        i += 1
    root.resizable(width=False, height=False)

create_frames_and_buttons()

root.mainloop()

结果:

这篇关于如何以像素为单位调整按钮的大小?(Tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆