【问题标题】:How to automatically resize and centre tkinter window如何自动调整 tkinter 窗口的大小和居中
【发布时间】:2021-02-19 23:36:14
【问题描述】:

当显示用户变量时,我在 tkinter 中调整窗口大小时遇到​​问题。由于程序采用团队的名称并稍后在程序中显示它,因此为显示窗口设置大小将不起作用,因为它需要自适应。我有让窗口居中的代码,但是有没有办法自动设置窗口的大小?

当前外观:

team_2 = t2_entry.get()
        top_2.withdraw()
        confirm_screen = Toplevel(master)
        w = 160 + len(team_2) + len(team_1)
        h = 50
        ws = confirm_screen.winfo_screenwidth()
        hs = confirm_screen.winfo_screenheight()
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        confirm_screen.geometry('%dx%d+%d+%d' % (w, h, x, y))
        t1_name = Label(confirm_screen, text = "Team 1: " + team_1).grid(row = 1, column = 1)
        t2_name = Label(confirm_screen, text="Team 2: " + team_2).grid(row = 1, column = 2)
        confirm_button = Button(confirm_screen, text = "Submit", width = 10, command = team_name_1).grid(row = 4, column = 1)
        redo = Button(confirm_screen, text="Redo", width=10, command= lambda: [start_team1(), clear()]).grid(row=4, column=2)

尝试 1:

team_2 = t2_entry.get()
        top_2.withdraw()
        confirm_screen = Toplevel(master)
        w = 160 + len(team_2) + len(team_1) 
        h = 50
        ws = confirm_screen.winfo_screenwidth()
        hs = confirm_screen.winfo_screenheight()
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        confirm_screen.geometry('%dx%d+%d+%d' % (w, h, x, y))
        t1_name = Label(confirm_screen, text = "Team 1: " + team_1).grid(row = 1, column = 1)
        t2_name = Label(confirm_screen, text="Team 2: " + team_2).grid(row = 1, column = 2)
        confirm_button = Button(confirm_screen, text = "Submit", width = 10, command = team_name_1).grid(row = 4, column = 1)
        redo = Button(confirm_screen, text="Redo", width=10, command= lambda: [start_team1(), clear()]).grid(row=4, column=2)

尝试 2:

team_2 = t2_entry.get()
        top_2.withdraw()
        confirm_screen = Toplevel(master)
        w = confirm_screen.winfo_width
        h = confirm_screen.winfo_height
        ws = confirm_screen.winfo_screenwidth()
        hs = confirm_screen.winfo_screenheight()
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        confirm_screen.geometry('%dx%d+%d+%d' % (w, h, x, y))
        t1_name = Label(confirm_screen, text = "Team 1: " + team_1).grid(row = 1, column = 1)
        t2_name = Label(confirm_screen, text="Team 2: " + team_2).grid(row = 1, column = 2)
        confirm_button = Button(confirm_screen, text = "Submit", width = 10, command = team_name_1).grid(row = 4, column = 1)
        redo = Button(confirm_screen, text="Redo", width=10, command= lambda: [start_team1(), clear()]).grid(row=4, column=2)

【问题讨论】:

    标签: python-3.x user-interface tkinter


    【解决方案1】:

    我想这就是你所追求的:

    from tkinter import *
    
    confirm_screen = Tk()
    t1_name = Label(confirm_screen, text = "Team 1: " + "team_1").grid(row = 1, column = 1)
    t2_name = Label(confirm_screen, text="Team 2: " + "team_2").grid(row = 1, column = 2)
    confirm_button = Button(confirm_screen, text = "Submit", width = 10, command = None).grid(row = 4, column = 1)
    redo = Button(confirm_screen, text="Redo", width=10, command= lambda: [start_team1(), clear()]).grid(row=4, column=2)
    
    # You need to update the display so that the widgets are actually displayed on the screen.
    # Otherwise `w` and `h` will both = 1
    confirm_screen.update()
    
    w = confirm_screen.winfo_width()
    h = confirm_screen.winfo_height()
    ws = confirm_screen.winfo_screenwidth()
    hs = confirm_screen.winfo_screenheight()
    x = (ws / 2) - (w / 2)
    y = (hs / 2) - (h / 2)
    
    confirm_screen.geometry('+%d+%d' % (x, y))
    

    您必须首先放置所有小部件,然后计算窗口应该去哪里。使用<tkinter.Tk>.geometry(...)时也不需要总是包含窗口的大小。

    【讨论】:

    • 这在调整窗口大小的意义上确实有效,但它似乎偏离了中心,并且更多地位于屏幕的右下角。我会坚持下去,并尝试找到一种方法来定位它。如果我这样做,请编辑您的评论并将其标记为答案
    • @EamonSharma 我想我修好了,在计算位置之前你需要一个confirm_screen.update()(它允许小部件显示自己然后计算窗口的大小)。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多