【问题标题】:having problem with positioning the tkinter widget定位 tkinter 小部件时遇到问题
【发布时间】:2021-09-18 08:53:50
【问题描述】:

Text 小部件的位置在此代码中位于左中。我希望它位于左侧标签小部件的下方。我试图通过 pady 参数定义位置,但它不起作用。如何更改文本小部件的位置。

from tkinter import *
from tkinter.font import Font

root = Tk()
root.title("Project")
root.geometry('700x400')

name = Label(root,text="Project")
name.configure(font=('Arial',20),fg='Blue',bg='yellow')
name.pack()

symbol_input = Text(root)
symbol_input.config(height=1,width=10)
symbol_input.pack(padx=5,side='left')

root.mainloop()

【问题讨论】:

  • 您是否尝试过使用grid 系统而不是pack?你可以得到更准确的结果。
  • 只使用 place 而不是 pack:name = Label(root,text="Project").place(x=10, y=45) 其中“x”是水平对齐方式...
  • place 很少是最好的解决方案。它有其用途,但与 gridpack 相比,它需要更多的工作才能获得响应式 GUI。
  • 尝试将symbol_input.pack(padx=5,side='left')更改为symbol_input.pack()

标签: python python-3.x tkinter


【解决方案1】:

您有两个选择:grid() 和 pack()。我个人更喜欢网格,因为它更方便我在框架上定位和放置东西。不管您选择什么,请确保在整个代码中保持相同,因为 Tkinter 不允许这样做。

name = Label(root, text="Project", font=('Arial',20), fg='Blue, bg='yellow')
name.grid(sticky=N + E + W + S)
name.grid_rowconfigure(0, weight=100)
name.grid_columnconfigure(0, weight=100)
name.place(relx=0.28, rely=0.3)

place() 方法允许您将标签小部件放置在根中的任何特定坐标处。我使用了 relx(相对 x)和依赖(相对 y),它们告诉小部件应该相对于它所在的根目录。

0

我建议使用 relx 和依赖,而不仅仅是 x 或 y,因为它允许您的程序在具有不同屏幕尺寸(甚至窗口大小调整)的其他设备上工作,而无需相对于根目录中的其他小部件进行任何重新定位。

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2019-09-12
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多