如何打开Tkinter窗口而不是开始最小化?

时间:2017-07-18 20:56:36

标签: python windows python-2.7 python-3.x tkinter

如何打开Tkinter窗口(例如条目,文本......)并在打开它们时使它们显示在屏幕上而不是开始最小化? 我真的不知道如何开始...我有一些窗户,但它们被打开最小化。我在互联网上搜索,但没有发现可能相关的任何内容。我该怎么做 ? 在Windows上使用python(Python 3和Python 2) 感谢您的帮助!

编辑:现在我在评论中提到的问题是我必须强制显示窗口。但是当我这样做时,即使我使用一个函数将集中在之前,窗口也不会开始居中。

代码:

def center(toplevel):
    toplevel.update_idletasks()
    w = toplevel.winfo_screenwidth()
    h = toplevel.winfo_screenheight()
    size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
    x = w/2 - size[0]/2
    y = h/2 - size[1]/2
    toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))


def paste_func():
    global text_box
    text_box.insert(END, top.clipboard_get())
    button_pressed()


def button_pressed(x=0):
    # This function determines which button was pressed, and closes this menu/message box/etc...
    global pressed
    pressed = x
    destroy_top()


def destroy_top():
    # This function closes this menu/message box/etc...
    global top
    top.iconify()
    top.withdraw()
    top.quit()

def get_text():
    global pressed
    global top
    global text_box

    pressed = 0
    top = Tk()
    top.withdraw()
    top.rowconfigure(0, weight=0)
    top.columnconfigure(0, weight=0)
    top.config(height=0, width=0)
    top.protocol('WM_DELETE_WINDOW', lambda: button_pressed(-1))

    text_box = Entry(top, width=50)
    text_box.focus_set()
    text_box.grid(row=0, column=0)
    but = Button(top, text='Enter', command=button_pressed)
    but.grid(row=0, column=1)
    paste = Button(top, text='Paste', command=paste_func)
    paste.grid(row=0, column=2)

    top.deiconify()
    text_box.focus_set()
    top.after(0, top.focus_force())
    center(top)
    top.mainloop()

    if pressed == -1:
        exit()

    return text_box.get('1.0', index2=END)

1 个答案:

答案 0 :(得分:1)

window.focus_force() method执行此操作:

  

强制输入焦点到窗口小部件。这是不礼貌的。最好等待窗口管理器为您提供焦点。另见下面的.grab_set_global()

有时如果这不起作用,你可以手动强制它:

from Tkinter import *

window = Tk()
window.after(2000, window.focus_force)
window.mainloop()

有时你会有issues on Macs which can require some additional finagling,但这应该可以在其他地方正常工作(OP没有指明任何有关环境的信息)。