Tkinter网格法

2024-10-02 10:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Tkinter为我的计算机科学课程创建一个基于隐写术的GUI。我正在使用窗口中的小部件上的.grid()函数来布置它们,但是我不能让这个特定的部分看起来像我想要的那样。在

下面是我的GUI当前的样子:http://imgur.com/LNEZtEL (或者只是有错误的部分)。在

我希望剩下的字符标签直接放在文本输入框的下面,但是出于某种原因,第4行在文本输入框的下面开始了很大的一段距离。如果我用列和行锚定在西北方向来标记GUI,它看起来像这样:http://imgur.com/a/V7dTW。在

如果我缩小左边的图像框,它看起来像我想要的,但是我不想要这么小的图像:http://imgur.com/a/0Dudu。在

图像框的行跨度为2,那么是什么原因导致第4行从文本输入框开始如此低?我希望GUI大致如下:http://imgur.com/a/ck04A。在

完整代码:

imageButton = Button(root, text="Add Image", command = add_image)
imageButton.grid(row = 2, columnspan = 2, sticky = W, padx = 30, pady = 20)
steg_widgets.append(imageButton)

image = Image.open("square.jpg")
image = image.resize((250,250))
photo = ImageTk.PhotoImage(image)
pictureLabel = Label(root, image = photo)
pictureLabel.image = photo
pictureLabel.grid(column = 0, row = 3, columnspan = 2, rowspan = 2, padx = 20, pady = (0, 20), sticky = NW)
steg_widgets.append(pictureLabel)

nameLabel = Label(root, text = "Brandon Edwards - OCR Computer Science Coursework 2016/2017")
nameLabel.grid(row = 0, column = 2, columnspan = 2, padx = (0, 20), pady = 10)
steg_widgets.append(nameLabel)

inputTextLabel = Label(root, text = "Enter text:")
inputTextLabel.grid(row = 2, column = 2, sticky = W)
steg_widgets.append(inputTextLabel)

startButton = Button(root, text="Go!", command = start_stega)
startButton.grid(row = 2, column = 2, sticky = E)
steg_widgets.append(startButton)

inputTextBox = Text(root, height = 10, width = 30)
inputTextBox.grid(row = 3, column = 2, sticky = NW)
steg_widgets.append(inputTextBox)

maxCharLabel = Label(root, text = "Remaining characters:")
maxCharLabel.grid(row = 4, column = 2, sticky = NW)
steg_widgets.append(maxCharLabel)

saveButton = Button(root, text="Save Image", command = save_image)
saveButton.grid(row = 2, column = 3, sticky = W)
steg_widgets.append(saveButton)

Tags: textimagecomhttpguicolumnrootwidgets
1条回答
网友
1楼 · 发布于 2024-10-02 10:31:21

我建议将用户界面分解为逻辑部分,并分别对每个部分进行布局。在

例如,右边和另一个部分有两个不同的小部件。首先为这两个组创建容器:

import Tkinter as tk
...
left_side = tk.Frame(root)
right_side = tk.Frame(root)

由于它们是并排的,pack是布置它们的最简单方法:

^{pr2}$

接下来,你可以只关注一个方面。您可以使用packgrid。这使用grid作为说明:

image = tk.Canvas(left_side, ...)
button = tk.Button(left_side, ...)

left_side.grid_rowconfigure(0, weight=1)
left_side.grid_columnconfigure(0, weight=1)
image.grid(row=0, column=0, sticky="nw")
button.grid(row=1, column=0, sticky="n")

最后,在右边工作。由于小部件是从上到下堆叠的,pack是自然选择:

l1 = tk.Label(right_side, text="Enter text:")
l2 = tk.Label(right_side, text="Remaining characters")
text = tk.Text(right_side)
l1.pack(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
l2.pack(side="top", fill="x")

相关问题 更多 >

    热门问题