如何在Tkinter中强制调整顶层窗口的大小?

2024-09-28 01:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我和特金特的冒险还在继续。 下面是一个演示代码:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import threading as thr
import ttk
import Tkinter as tk
import Queue

import loop_file


class MyApp(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.model_queue = Queue.PriorityQueue()
        self.butt = ttk.Button(self, text='Click me!', command=self.proc)
        self.butt.grid()

    def finish_loop_window(self):
        # remove "Wait" message
        self.waitFr.destroy()
        # create Frames for Toplevel window
        graphFr = ttk.Frame(self.loop_win, borderwidth=2, relief='groove')
        graphFr.grid(row=0, column=0, sticky='nsew')

        # update to get the new dimensions
        self.update()
        self.loop_win.update()
        newsize = self.loop_win.winfo_geometry()
        print newsize
        ttk.Label(self.loop_win, text=self.fib_result).grid()
        self.loop_win.geometry(newsize)  # here I expected the window to be resized


    def check_looping_thread_save_results(self):
        """
        Check every 10ms if loop thread is alive.
        """
        if self.loop_thread.is_alive():
            self.after(10, self.check_looping_thread_save_results)
        else:
            self.loop_thread.join()
            # get the results of model processing
            self.fib_result = self.model_queue.get()
            self.finish_loop_window()

    def centrify_widget(self, widget):
        widget.update_idletasks()
        width = widget.winfo_screenwidth()
        height = widget.winfo_screenheight()
        xy = tuple(int(c) for c in widget.geometry().split('+')[0].split('x'))
        xpos = width/2 - xy[0]/2
        ypos = height/2 - xy[1]/2
        widget.geometry("%dx%d+%d+%d" % (xy + (xpos, ypos)))

    def proc(self):
        self.model_queue = Queue.PriorityQueue()
        self.loop_thread = thr.Thread(target=loop_file.do_looping,
                                        args=(self.model_queue, ))
        self.loop_thread.start()
        self.after(10, self.check_looping_thread_save_results)

        self.loop_win = tk.Toplevel()
        self.loop_win.title('Toplevel')
        self.waitFr = ttk.Frame(self.loop_win, borderwidth=2, relief='groove')
        self.waitFr.grid(sticky='nsew')
        ttk.Label(self.waitFr, text='Wait...\nCreating plots...').grid()
        self.centrify_widget(self.loop_win)


root = tk.Tk()
root.title('Test')
root.update()
gui = MyApp(root)
gui.mainloop()

外部loop_file

^{pr2}$

如果您运行这个小演示,您将看到一个Click me!按钮。如果您单击它,将出现一个弹出窗口Wait... Creating plots...,并将运行一个单独的线程。线程完成后,窗口将更新,但未调整大小。在

enter image description hereenter image description hereenter image description here

更新后如何调整窗口大小?在


Tags: importselfloopmodelqueuedefupdatewidget
1条回答
网友
1楼 · 发布于 2024-09-28 01:24:24

如果我正确地理解了这个问题,你希望弹出窗口缩小以适应新内容。在

如果是这样的话,您不必做任何事情,只需移除之前在窗口上设置的大小约束。当您使用参数调用geometry时,这将覆盖该窗口中可能发生的任何自然重新调整大小。你要求它是一个特定的尺寸,所以tkinter努力实现这个愿望。在

如果向geometry传递一个空字符串,tkinter将删除该约束,并让窗口增大或缩小以适应其内容。在

试试这个:

def finish_loop_window(self):
    # remove "Wait" message
    self.waitFr.destroy()

    # remove the fixed size of the window that was previously applied
    self.loop_win.geometry("")

    # create Frames for Toplevel window
    graphFr = ttk.Frame(self.loop_win, borderwidth=2, relief='groove')
    graphFr.grid(row=0, column=0, sticky='nsew')
    ttk.Label(self.loop_win, text=self.fib_result).grid()

相关问题 更多 >

    热门问题