首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python Tkinter中的网格无缘无故地调整大小?

Python Tkinter中的网格无缘无故地调整大小?
EN

Stack Overflow用户
提问于 2018-07-12 03:24:24
回答 1查看 26关注 0票数 0

我正在用Python创建一个待办事项列表,并且我必须在开始时安排3个元素。其中包括一个笔记本,以便翻阅笔记和待办事项。其他的只是上面有图像的标签。下面是代码。我的问题是,我不确定我应该如何将这些图像放到网格上。Notebook位于第1行,第0列,它非常大,导致第2列一直向右移动,这使得无法在第1行中将两个项目放在彼此靠近的列中。以下是代码。

代码语言:javascript
复制
from tkinter import *
import datetime
from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText

root = Tk()
root.title("To - Do List")
root.geometry("1200x600")
root.configure(background = "white")
# Variable list:

content = ttk.Frame(root)
content.grid(column=0, row=0, sticky=(N, S, E, W))


Photo1= PhotoImage(file="Full Circle Image Icon Docs.png")


Photo2 = PhotoImage(file="To - Do List Label.png")


TasksList = ttk.Notebook()
Task1 = ttk.Frame(TasksList)
Task2 = ttk.Frame(TasksList)
TasksList.add(Task1, text = 'One')
TasksList.add(Task2, text = 'Two')
TasksList.grid(row=2, column=0, sticky=W)

root.columnconfigure(0, weight=0)
root.rowconfigure(0, weight=0)

Label(image=Photo1, bg="black", borderwidth=0, highlightthickness=0).grid(row=0, column=0, sticky=W)
Label(image=Photo2, borderwidth=0, highlightthickness=0).grid(row=0, column=1, sticky=W)
root.mainloop()

我们将非常感谢您的帮助。非常感谢各位!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-12 05:19:13

将网格设置为三列宽,然后将column weigth=1设置为第三列,以使其随窗口展开。然后将标签放在第一列和第二列中。

然后让TasksList用:columnspan=3填充所有三列。

此外,我将TasksList放在第二行,并让它具有weight=1以随窗口展开。请参阅此示例:

代码语言:javascript
复制
from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("500x600+800+50")
# Set which cols and rows should expand with window
root.columnconfigure(2, weight=1)
root.rowconfigure(1, weight=1)

TasksList = ttk.Notebook(root)
Task1 = ttk.Frame(TasksList)
Task2 = ttk.Frame(TasksList)
TasksList.add(Task1, text = 'One One One One One One')
TasksList.add(Task2, text = 'Two Two Two Two Two Two')
TasksList.grid(row=1, column=0, sticky=N+W, columnspan=3)
# Make the TaskList span all three columns ------^

Photo1= PhotoImage(file="test.gif")
Photo2 = PhotoImage(file="test.gif")
# Put labels in root
Label(root, image=Photo1, bd=0).grid(row=0, column=0, sticky=W)
Label(root, image=Photo2, bd=0).grid(row=0, column=1, sticky=W)
# Don't put anything in column=2

root.mainloop()

在row=0中,第三列(空)将展开以填充窗口。在row=1中,TasksList将展开以填充窗口。有关详细信息,请参阅The Tkinter Grid Geometry Manager

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51292880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档