我和特金特的冒险还在继续。 下面是一个演示代码:
#!/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
:
如果您运行这个小演示,您将看到一个Click me!
按钮。如果您单击它,将出现一个弹出窗口Wait... Creating plots...
,并将运行一个单独的线程。线程完成后,窗口将更新,但未调整大小。在
更新后如何调整窗口大小?在
如果我正确地理解了这个问题,你希望弹出窗口缩小以适应新内容。在
如果是这样的话,您不必做任何事情,只需移除之前在窗口上设置的大小约束。当您使用参数调用
geometry
时,这将覆盖该窗口中可能发生的任何自然重新调整大小。你要求它是一个特定的尺寸,所以tkinter努力实现这个愿望。在如果向
geometry
传递一个空字符串,tkinter将删除该约束,并让窗口增大或缩小以适应其内容。在试试这个:
相关问题 更多 >
编程相关推荐