框架内的网格?

2023-07-23Python开发问题
13

本文介绍了框架内的网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否可以将 Tkinter 中的按钮网格放置在另一个框架内?

Is it possible to place a grid of buttons in Tkinter inside another frame?

我想创建一个类似于井字游戏的游戏,并希望使用网格功能来放置游戏方块(即按钮).但是,我想在 GUI 中添加除游戏板之外的其他内容,因此将所有内容都放在一个网格中并不理想.

I'm wanting to create a tic-tac-toe like game and want to use the grid feature to put gamesquares (that will be buttons). However, I'd like to have other stuff in the GUI other than just the game board so it's not ideal to just have everything in the one grid.

举例说明:

O | X | X   |
----------  |
O | O | X   | Player 2 wins!
----------  | 
X | O | X   |

井字棋盘位于由所有按钮组成的网格中,玩家 2 获胜"是框架内的标签.

The tic tac toe board is in a grid that is made up of all buttons and the 'player 2 wins' is a label inside a frame.

这是对我试图做的事情的过度简化,请耐心等待,因为到目前为止我设计程序的方式(板是动态创建的)网格最有意义.

This is an oversimplification of what I'm trying to do so bear with me, for the way I've designed the program so far (the board is dynamically created) a grid makes the most sense.

有一个想法,但是当我运行它时,什么也没发生?如果我取出框架位,它会.有什么想法吗?

Had a thought but when I run it, nothing happens? If I take out the frame bit it does. Any ideas?

from Tkinter import * 

root = Tk()

b = Button(root, text = "1")
b.grid(row=1, column=3)
b2 = Button(root, text = "2")
b2.grid(row=1, column=4)

f = Frame(root, bg = "red")
f.pack(side=RIGHT)

root.mainloop()

推荐答案

终于想出了一个办法:

from Tkinter import * 

root = Tk()

f = Frame(root, bg = "orange", width = 500, height = 500)
f.pack(side=LEFT, expand = 1)

f3 = Frame(f, bg = "red", width = 500)
f3.pack(side=LEFT, expand = 1, pady = 50, padx = 50)

f2 = Frame(root, bg = "black", height=100, width = 100)
f2.pack(side=LEFT, fill = Y)

b = Button(f2, text = "test")
b.pack()

b = Button(f3, text = "1", bg = "red")
b.grid(row=1, column=3)
b2 = Button(f3, text = "2")
b2.grid(row=1, column=4)
b3 = Button(f3, text = "2")
b3.grid(row=2, column=0)

root.mainloop()

将网格放在框架内的框架内是一种让网格周围的填充工作的技巧,但它可以工作,所以我很高兴.

Having the grid inside a frame inside a frame is a bit of a hack to get the padding around the grid working but it works so I'm happy.

这篇关于框架内的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10