【问题标题】:Grid in Python Tkinter is Resizing Without Reason?Python Tkinter 中的网格无故调整大小?
【发布时间】:2018-12-19 22:21:36
【问题描述】:

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

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()

我们将不胜感激。非常感谢各位!

【问题讨论】:

    标签: python python-3.x tkinter grid character-replacement


    【解决方案1】:

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

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

    我还把 TasksList 放在第二行,让它的 weight=1 随窗口展开。看这个例子:

    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 了解更多信息。

    【讨论】:

    • 非常感谢!
    • 如果您认为我的回答对您有用,请接受。
    猜你喜欢
    • 2017-04-07
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多