2017-07-18 86 views
0

如何打開Tkinter窗口(例如條目,文本等),並在打開時讓它們出現在屏幕上而不是開始最小化? 我真的不知道如何開始......我有一些窗戶,但他們打開最小化。我在互聯網上搜索,但沒有發現任何可能相關的東西。我該怎麼做 ? 在windows上使用python(Python 3和Python 2) 感謝您的幫助!如何使Tkinter窗口在打開時顯示而不是開始最小化?

編輯:現在的問題,因爲我在這裏的評論中提到的是,我必須強制顯示窗口。但是當我這樣做的時候,即使我使用一個函數來集中它以前工作的,窗口也不會開始居中。

代碼:

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) 
+0

讓我們看看您的代碼,我們可能會更好地理解,通常Windows並未創建爲最小化。 – Max

+0

Windows始終以非最小化狀態啓動。你必須明確地將它們最小化。你能否提供[mcve]來說明問題。 –

+0

這是一個完全不同的問題,而不是我回答的問題。 – Dan

回答

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尚未指定有關環境的任何信息) 。

+0

謝謝!作品像魅力 – Lidor

+0

@BryanOakley好點。它需要一個回調,我習慣於想傳遞它的參數,所以我使用lambda來做到這一點。在這種情況下,不需要,因爲沒有參數。編輯回答 – Dan

+0

原諒我高手!我想我知道爲什麼會出現這個問題,這是因爲我使用了撤銷和解密。但現在我又遇到了另外一個問題,我怎樣才能使用退出,什麼時候需要使用deiconify,打開窗口並讓它出現在屏幕上?由於某種原因,如果我讓它出現在屏幕上,就像你做的那樣,它不會打開居中......(早些時候它已經) – Lidor

相關問題