【发布时间】:2019-06-04 17:47:31
【问题描述】:
我实际上正在学习 Tkinter,我想做一个看起来像这样的窗口(俄罗斯方块 GUI): image 所以我想做一个 20x15 网格(h x w),平铺大小为 25px(所以 500px x 375px playArea 大小) 所以这是我制作的代码:
from tkinter import *
from random import randint
root = Tk()
root.resizable(False, False)
# PLAYAREA SIZE
HEIGHT = 500
WIDTH = HEIGHT // 2
TILE_SIZE = 25
playarea = Canvas(root, width=WIDTH, height=HEIGHT, bg='yellow')
playarea.grid(column=6, row=0, columnspan=WIDTH // TILE_SIZE, rowspan=HEIGHT // TILE_SIZE)
menuFrame=Frame(root, width=(WIDTH // 2), height=HEIGHT).grid(column=0, row=0, columnspan= (WIDTH // 2) // TILE_SIZE , rowspan= int(HEIGHT / TILE_SIZE))
newGameButton = Button(menuFrame, text='Start', width= 75 , height = 1 * TILE_SIZE)
newGameButton.grid(column=1, row=1,columnspan=3, rowspan= 1)
newTestButton = Button(menuFrame, text='Test', width= 5 * TILE_SIZE, height = 1* TILE_SIZE)
newTestButton.grid(column=0, row=2, columnspan=5, rowspan=1)
root.update()
print(newGameButton.winfo_width())
print(playarea.grid_size())
#print(playarea.grid_info())
root.mainloop()
我使用 HEIGHT 和 WIDTH 变量来定义画布的大小,并使用 TILE_SIZE 变量来定义需要的图块数量(配置 columnspan 和 rowspan)我的第一个问题是我的:
print(playarea.grid_size())
返回 (0,0),因为我配置了 columnspan 和 rowspan 它应该返回我 (10,20),不是吗?
然后另一个问题是我的按钮,在这里我创建了两个按钮(newGame 和 test),我希望 newGame 的宽度为 75 像素(或 3 * TILE_SIZE),但是当我运行它时(宽度 = 75和宽度 = 3 * TILE_SIZE)
print(newGameButton.winfo_width())
返回 617px 并且按钮很大,我不明白为什么,有人可以解释一下网格是如何工作的吗?这是什么原因造成的。
【问题讨论】: