如何在Tkinter中使用网格水平拉伸细胞? [英] How to stretch cell horizontally using grid in Tkinter?

查看:95
本文介绍了如何在Tkinter中使用网格水平拉伸细胞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时使用tkinter来制作简单的GUI,我想使其变得过于简单和酷,所以我想将此标签**********填入屏幕,但是fill在网格中像在打包一样,所以我正在寻找替代方案.

I was using tkinter to make simple GUI and I wanted to make it too simple and cool so I wanted to this label ********** to be filled in screen but the there is no option for fill in grid like in pack so I am looking for alternative.

这是代码:

import tkinter
import os


def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid()

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


tkinter.Button(root, text="Submit", command=submit).grid(row=4, column=1)

root.mainloop()
 

我要填写的标签在24行中.

The label i am trying to fill is in the 24 line.

推荐答案

您正在搜索列跨度和粘性:

You're searching for columnspan and sticky:

import tkinter
import os


def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid(columnspan=2, sticky='ew')

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


tkinter.Button(root, text="Submit", command=submit).grid(row=4, column=1)

root.mainloop()

在此行(25)中

lab.grid(columnspan=2, sticky='ew')

我使用了columnspan,它告诉tkinter合并给定数量的. sticky告诉tkinter,此小部件坚持向东和向西'ew'.您会发现带有sticky='nsew'巫婆的代码,意味着北,南,东和西.

I used columnspan that tells tkinter to merge columns in given number. The sticky tells tkinter that this widget sticks to 'ew' east and west. You will find code with sticky='nsew' witch means north, south, east and west.

并且从文档中:

-columnspan n 插入从站,使其在网格中占据n列.默认值为一列,除非窗口名称后跟- 在这种情况下,列跨度会立即立即增加一次 跟随-.

-columnspan n Insert the slave so that it occupies n columns in the grid. The default is one column, unless the window name is followed by a -, in which case the columnspan is incremented once for each immediately following -.

粘性样式 如果从属单元大于其请求的尺寸,则可以使用此选项在从属单元内定位(或拉伸)从属单元. 样式是包含零个或多个字符n,s,e的字符串 或w.该字符串可以选择包含空格或逗号,但是它们 被忽略.每个字母指的是一面(北,南,东或 西),奴隶将坚持"下去.如果n和s(或e和w)都为 指定,从属将被拉伸以填满整个高度(或 腔的宽度). -sticky选项包含以下内容的组合: 包使用的-anchor和-fill.默认值为",这会使从设备以其请求的大小在其腔中居中.

-sticky style If a slave's cell is larger than its requested dimensions, this option may be used to position (or stretch) the slave within its cell. Style is a string that contains zero or more of the characters n, s, e or w. The string can optionally contains spaces or commas, but they are ignored. Each letter refers to a side (north, south, east, or west) that the slave will "stick" to. If both n and s (or e and w) are specified, the slave will be stretched to fill the entire height (or width) of its cavity. The -sticky option subsumes the combination of -anchor and -fill that is used by pack. The default is "", which causes the slave to be centered in its cavity, at its requested size.

更新

您的关注问题是:

Update

Your follw quesion is:

它在某种程度上起作用了,但是我正在寻找替代品来填补 columnpan仅对2生效,我尝试了其他值,但其他 没有效果

It worked to some extend but I was look for alternative for fill the columnspan only takes effect with 2 I tried other values but others have no effect

根据您的代码,最好的选择是使用tkinter的grid_columnconfigure(*column, *weight) 方法. 可以在此处找到一个很好的解释.

Based on your Code you the best option is use the grid_columnconfigure(*column, *weight) method of tkinter. A perfectly fine explaination can be found here.

为什么这是您的最佳选择? 仅仅是因为您使用了tkinter的几何方法. 那么它是如何工作的?

Why is this the best choice in your case ? Just because you used the geometry method of tkinter. So how does it work?

tkinter会自行计算窗口的大小.因此,您也可以只删除几何方法的线条以产生相似的效果.

tkinter calculate the size of the window by itself if nothing is predefined by the developer. Therefore you could also just delete your lines of the geometry method to have a simliar effect.

但是由于您已经预先确定了几何形状,所以您已经建立了空间. tkinter的网格方法还可以通过它包含的最大窗口小部件自动计算列或行的大小.因此,如果您拥有的最大窗口小部件是 configure (width=100),并且宽度以像素为单位定义,则每个 column width 为100像素.与 row height相同.

But since you had predifned the geometry you had built space. The grid method of tkinter also calculate the size of a column or a row automatically by the biggest widget it contains. So if the biggest widget you have is configured with (width=100) and the width is defined in pixel you would get each column with a width of 100 pixel. Same as row with height.

这就是为什么您需要使用tkinter的grid_columnconfigure方法的原因. 简而言之,此方法告诉tkinter哪个窗口小部件最先获得空间,如果空间为空.

That's why you need to use the grid_columnconfigure method of tkinter. In short this method tells tkinter which widget have wich priority of getting space, if there is empty space.

在我编辑过的下面可以找到完整的方言:

A full exampel can be found below I edited:

  1. 根列配置.
  2. 将按钮绑定到变量.
  3. 将网格方法分离为变量.因为每种方法 返回一些东西. 更多
  4. 编辑了网格参数,以使其与(column=0, columnspan=2)居中
  1. the root columnconfigure.
  2. bound your button to a variable.
  3. seperated the grid method form the variable. Because every method returns something. more
  4. edited the grid parameter to center it with (column=0, columnspan=2)

import tkinter
import os

  
def submit():
    os.chdir("/home/samip/forms")
    with open(f'{user_val.get()}.txt', 'w') as file:
        file.write(f'''Person Details
        User: {user_val.get()}
        Age: {age_val.get()}
        Address: {address_val.get()}
        ''')
        quit()




root = tkinter.Tk()
root.geometry("400x200")
root.maxsize(400, 200)
root.minsize(400, 200)
root.title("Mars form")

lab = tkinter.Label(text="Mars Form", font=("Arial", 15, "bold"), bg="orange", fg="white", padx=10)
lab.grid(columnspan=2, sticky='ew')
root.columnconfigure(0, weight=1)

user = tkinter.Label(text="Name:")
age = tkinter.Label(text="Age:")
address = tkinter.Label(text="Address:")

user.grid()
age.grid()
address.grid()
user_val = tkinter.StringVar()
age_val = tkinter.StringVar()
address_val = tkinter.StringVar()

user_val_entry = tkinter.Entry(root, textvariable=user_val)
age_val_entry = tkinter.Entry(root, textvariable=age_val)
address_val_entry = tkinter.Entry(root, textvariable=address_val)
user_val_entry.grid(row=1, column=1)
age_val_entry.grid(row=2, column=1)
address_val_entry.grid(row=3, column=1)


b1 = tkinter.Button(root, text="Submit", command=submit)
b1.grid(row=4, ,column=0, columnspan=2)

root.mainloop()

现在,您应该了解tkinter网格方法的所有基本信息,以便编制出GUI .玩得开心!

Now you should know all the basic information of tkinter grid method to be abel to work your GUI out. Have fun!

这篇关于如何在Tkinter中使用网格水平拉伸细胞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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