简体   繁体   English

Python 2.7 / Windows 7 / Tkinter:父级上的中心临时子窗口

[英]Python 2.7 / Windows 7 / Tkinter: Center transient child window over parent

As I understood from the Tkinter reference, a transient child window is drawn (centered?) on its parent window. 正如我从Tkinter参考中所理解的那样,在其父窗口上绘制了一个瞬态子窗口(居中?)。 This works for me in Linux but not in Windows (7). 这适用于Linux,但不适用于Windows(7)。

Do you have any suggestions why this happens? 你有什么建议为什么会这样吗?

I can center the window manually with the .geometry() method, but when the GUI is calculating some values (say for a second) then there is a small window appearing with no GUI at all which then flits to the specified position even when I call the .withdraw method. 我可以使用.geometry()方法手动居中窗口,但是当GUI计算某些值(比如一秒钟)时,会出现一个没有GUI的小窗口,然后即使在我的情况下也会移动到指定位置调用.withdraw方法。

Thanks a lot! 非常感谢!

# -*- coding: utf-8 -*-

import Tkinter as tk
import ttk

class Parent(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.txt = tk.Text(master=self)
        self.txt.pack()
        self.btn = ttk.Button(master=self, text='Show Child',
                              command=self.show)
        self.btn.pack()
        self.mainloop()

    def show(self, *args):
        Child(self)


class Child(tk.Toplevel):

    def __init__(self, master, *args, **kwargs):
        tk.Toplevel.__init__(self, *args, **kwargs)
        self.txt = tk.Text(master=self)
        self.txt.pack()

Parent()

Okay I solved this the following way: 好的,我通过以下方式解决了这个问题:

  • .withdraw() the window .withdraw()窗口
  • set the .geometry() to center over parent window .geometry()设置为父窗口的中心
  • do some calculation to get the desired UI 做一些计算以获得所需的UI
  • .update_idletasks()
  • finally .deiconify() the window. 最后.deiconify()窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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