当前位置: 动力学知识库 > 问答 > 编程问答 >

How to set a certain amount of rows and columns of a Tkinter grid?

问题描述:

so I'm trying to create a game of blackjack for my A Level coursework (well, I'm in the early stages) and I'm using a Tkinter grid to position all of my widgets.

I want a grid with 20 columns and 10 rows.

However, in my program, I only use columns 1 to 18 and rows 1 to 18.

How do I tell Tkinter that I still want the columns 0 and 19 and rows 0 and 9 as blank so it doesn't think that I want my grid to have only 9 rows and 19 columns?

Let's say I have this as my basic program:

from tkinter import *

window=Tk()

window.pack()

l=Label(window, text='Label1').grid(column=8, row=1)

l2=Label(window, text='Label2').grid(column=1, row=8)

So, in this program the grid would think that it needs to have only 9 columns and 9 rows but I actually want a column number 10 (9) as a blank space and a row number 10 (9) as a blank space as well.

Please help, I have to finish this program in two weeks.

网友答案:

As default empty row/column has height/width 0 (zero) and you don't see it (but it exists).

You can set minimal size for row or column (ie. row/column number 9)

 window.rowconfigure(9, {'minsize': 30})
 window.columnconfigure(9, {'minsize': 30})

 # or

 window.rowconfigure(9, minsize=30)
 window.columnconfigure(9, minsize=30)

Or you can put Label without text or with spaces as text.


import tkinter as tk

window = tk.Tk()

l1 = tk.Label(window, text='Label1')
l1.grid(column=8, row=1)

l2 = tk.Label(window, text='Label2')
l2.grid(column=1, row=8)

# add empty label in row 0 and column 0
l0 = tk.Label(window, text='     ')
l0.grid(column=0, row=0)

# set minimal size for row 9 and column 9
window.rowconfigure(9, {'minsize': 30})
window.columnconfigure(9, {'minsize': 30})

window.mainloop()
分享给朋友:
您可能感兴趣的文章:
随机阅读: