繁体   English   中英

如何使 Tkinter 中的 GUI 可扩展

[英]How to make a GUI in Tkinter scalable

我正在尝试为项目创建 GUI。 代码如下。 我有一个关于可扩展性的问题。 在我的脚本中,我得到 Tkinter 以全屏显示 GUI。 然而,由于 relx 的性质和依赖就地,它使 label 或按钮从相对的 x 和 y 位置开始。 然而,这将意味着在不同的屏幕上它看起来不一样。 例如,作为 relx 和依赖指示小部件从哪里开始(而不是中间),每次更改屏幕尺寸时,外观都会发生变化。 我展示了两张我认为可以表达我观点的图片。 在左边的图像中,我已经设法将小部件 position 放在中间,但是由于缩小时小部件开始位置的性质,关闭小部件不再位于中间。 请注意,它仍然以相同的 relx 和依赖开始,但不再位于中间。

在此处输入图像描述

本质上,有一种方法可以使其可扩展,因此无论 window 的大小如何,它看起来都是一样的。 这可以通过确保 relx 和依赖指示中间的 position 而不是小部件的开始来完成吗?

from tkinter import *
import tkinter.font as tkFont

root = Tk()
root.title("N Body Simulation")

def exitclick():
    root.destroy()

class FullScreenApp(object):
def __init__(self, master, **kwargs):
    self.master = master
    pad = 3
    self._geom = '200x200+0+0'
    master.geometry("{0}x{1}+0+0".format(
        master.winfo_screenwidth() - pad, master.winfo_screenheight() - pad))
    master.bind('<Escape>', self.toggle_geom)

def toggle_geom(self, event):
    geom = self.master.winfo_geometry()
    print(geom, self._geom)
    self.master.geometry(self._geom)
    self._geom = geom


frame = Frame(root)
frame.pack()

fontStyle = tkFont.Font(family="Lucida Grande", size=20)
text_intro = "This is the GUI for the N-Body simulation, from here you can change the plents and the initial conditions"
label = Label(root, text=text_intro, font=fontStyle)
label.place(relx=0.1, rely=0.1)

close_button = Button(root, text="Close", command=exitclick, height=10, width = 30)
# close_button.place(relx=0.8, rely=20)
close_button.place(relx=0.4, rely=0.8)


e = Entry(root, width=35, borderwidth=5)
app = FullScreenApp(root)

root.mainloop()

您可以使用anchor选项来控制小部件的哪一部分位于给定的 position 处。

这是 tcl/tk 文档中anchor的规范文档,针对 tkinter 稍作调整:

anchor where - where指定 window 的哪个点将定位在由xyrelxrely选项选择的 (x,y) 位置。 锚点是指 window 的外部区域,包括其边界(如果有)。 因此,如果 where 是se ,则窗口边框的右下角将出现在主窗口中给定的 (x,y) 位置。 锚 position 默认为nw

anchor的允许值是字符串nwwswsseenencenter

例如,这将使 label 居中,无论 window 的大小是多少,方法是将小部件的中心放置在相对坐标 5/.5 处:

label = tk.Label(root, hello, world)
label.place(relx=.5, rely=5. anchor="center")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM