简体   繁体   English

如何使 Tkinter 中的 GUI 可扩展

[英]How to make a GUI in Tkinter scalable

I am trying to create a GUI for a project.我正在尝试为项目创建 GUI。 The code for which is below.代码如下。 I have a question regarding scalability.我有一个关于可扩展性的问题。 In my script I get Tkinter to display the GUI in fullscreen.在我的脚本中,我得到 Tkinter 以全屏显示 GUI。 However due to the nature of relx and rely in place it gets the label or button to start at the relative x and y positions.然而,由于 relx 的性质和依赖就地,它使 label 或按钮从相对的 x 和 y 位置开始。 However this will mean on a different screen it won't look the same.然而,这将意味着在不同的屏幕上它看起来不一样。 For instance as relx and rely dictate where the widget starts (not the middle), every time the screen size is changed the appearance changes.例如,作为 relx 和依赖指示小部件从哪里开始(而不是中间),每次更改屏幕尺寸时,外观都会发生变化。 I have shown two images which I think display my point.我展示了两张我认为可以表达我观点的图片。 On the left image I have managed to position the widget so its in the middle, however due to the nature of where the widget starts when shrunk the close widget is no longer in the middle.在左边的图像中,我已经设法将小部件 position 放在中间,但是由于缩小时小部件开始位置的性质,关闭小部件不再位于中间。 Note it still starts at the same relx and rely but is no longer in the middle.请注意,它仍然以相同的 relx 和依赖开始,但不再位于中间。

在此处输入图像描述

Essentially is there a way to make it scalable so it appears the same regardless of the size of window.本质上,有一种方法可以使其可扩展,因此无论 window 的大小如何,它看起来都是一样的。 Could this be done by making sure the relx and rely dictate the position of the middle not start of the widget?这可以通过确保 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()

You can use the anchor option to control which part of the widget is at the given position.您可以使用anchor选项来控制小部件的哪一部分位于给定的 position 处。

This is the canonical documentation for anchor from the tcl/tk documentation tweaked slightly for tkinter:这是 tcl/tk 文档中anchor的规范文档,针对 tkinter 稍作调整:

anchor where - where specifies which point of window is to be positioned at the (x,y) location selected by the x , y , relx , and rely options. anchor where - where指定 window 的哪个点将定位在由xyrelxrely选项选择的 (x,y) 位置。 The anchor point is in terms of the outer area of window including its border, if any.锚点是指 window 的外部区域,包括其边界(如果有)。 Thus if where is se then the lower-right corner of window's border will appear at the given (x,y) location in the master.因此,如果 where 是se ,则窗口边框的右下角将出现在主窗口中给定的 (x,y) 位置。 The anchor position defaults to nw .锚 position 默认为nw

The allowable values for anchor are the strings nw , w , sw , s , se , e , ne , n , and center . anchor的允许值是字符串nwwswsseenencenter

For example, this will keep a label centered no matter what size the window is by placing the center of the widget at the relative coordinates.5/.5:例如,这将使 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