简体   繁体   English

如何使Tkinter窗口在打开时出现而不是最小化?

[英]how to make Tkinter windows appear when opened rather than start minimized?

How can I open Tkinter windows (such as entry, text...) and make them appear on the screen when they are opened rather than start minimized ? 如何打开Tkinter窗口(例如条目,文本...)并使它们在打开时出现在屏幕上而不是最小化? I don't really know how to start... I have some windows but they are opened minimized. 我真的不知道该如何开始...我有一些窗户,但是它们被最小化了。 I searched on the internet, but found nothing that may be relevant. 我在互联网上搜索,但没有发现任何可能相关的内容。 how can I do it ? 我该怎么做 ? using python on windows (both Python 3 and Python 2) thanks for the help in advance ! 在Windows上使用python(Python 3和Python 2)都非常感谢您的帮助!

EDIT: the problem now as I mentioned in a comment here is that I have to force the window to be showed. 编辑:现在,我在这里的评论中提到的问题是我必须强制显示窗口。 But when I do so, the window does not start centered even if I use a function to center it that worked before. 但是,当我这样做时,即使我使用一个使之前工作的函数居中,窗口也不会居中

code: 码:

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)

The window.focus_force() method does this: window.focus_force()方法执行此操作:

Force the input focus to the widget. 将输入焦点强制到小部件。 This is impolite. 这是不礼貌的。 It's better to wait for the window manager to give you the focus. 最好等待窗口管理器为您提供焦点。 See also .grab_set_global() below. 另请参见下面的.grab_set_global()

Sometimes if this doesn't work, you can manually force it like so: 有时,如果这不起作用,您可以手动将其强制为:

from Tkinter import *

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

Sometimes you will have issues on Macs which can require some additional finagling but this should work fine elsewhere (OP has not specified any information about environment). 有时,您在Mac上会遇到问题,可能需要进行一些其他修改,但这在其他地方也可以正常使用(OP未指定任何有关环境的信息)。

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

相关问题 TkInter:如何使对象出现在第二个而不是第一个窗口上? - TkInter: how can I make objects appear on my second window rather than the first? 当单击而不是 shell 时,如何使按钮的值显示到 tkinter 上? - How to make value from button be displayed onto tkinter, when clicked rather than shell? 启动 Python 脚本 GUI 最小化/在 Windows 托盘中 | 特金特 - Start Python script GUI minimized / in Windows Tray | Tkinter Python-Tkinter:当窗口最小化到任务栏时,如何在Windows XP的任务栏上突出显示项目 - Python-Tkinter:how to highlight item on taskbar on windows xp when window is minimized to the taskbar python) 当按下按钮时,如何使 tkinter 出现? - python) How do I make tkinter appear when a button is pressed? Tkinter:如何在按下按钮时使窗口出现 - Tkinter: how to make window appear when a button is pressed 我将如何制作一个无法在 Python Tkinter 中最小化的 tk 窗口? - How will I make a tk window which cannot be minimized in Python Tkinter? 从任务栏单击时,Tkinter 使覆盖重定向窗口出现在其他窗口的顶部 - Tkinter make overrideredirect window appear on top of other windows when clicked from taskbar 如何使 Python 开始一个新行而不是切断单词 - How to make Python start a new line rather than cutting words off 如何使tkinter对话框首先出现? - How to make tkinter dialog box appear first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM