简体   繁体   中英

How to make a GUI in Tkinter scalable

I am trying to create a GUI for a project. The code for which is below. I have a question regarding scalability. In my script I get Tkinter to display the GUI in fullscreen. 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. 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. 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. Note it still starts at the same relx and rely but is no longer in the middle.

在此处输入图像描述

Essentially is there a way to make it scalable so it appears the same regardless of the size of window. Could this be done by making sure the relx and rely dictate the position of the middle not start of the widget?

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.

This is the canonical documentation for anchor from the tcl/tk documentation tweaked slightly for 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. The anchor point is in terms of the outer area of window including its border, if any. 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. The anchor position defaults to nw .

The allowable values for anchor are the strings nw , w , sw , s , se , e , ne , n , and center .

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 = tk.Label(root, hello, world)
label.place(relx=.5, rely=5. anchor="center")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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