tkinter:如何在创建i时不显示空的根窗口

2024-09-27 20:17:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个创建窗口的简单脚本:

import Tkinter as tk

def center(win):
  win.update_idletasks()
  width = win.winfo_width()
  frm_width = win.winfo_rootx() - win.winfo_x()
  win_width = width + 2 * frm_width
  height = win.winfo_height()
  titlebar_height = win.winfo_rooty() - win.winfo_y()
  win_height = height + titlebar_height + frm_width
  x = win.winfo_screenwidth() // 2 - win_width // 2
  y = win.winfo_screenheight() // 2 - win_height // 2
  win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

def showDialog():
  print "tkinter"
  root = tk.Tk()
  root.title("Say Hello")
  label = tk.Label(root, text="Hello World")
  label.pack(side="top", fill="both", expand=True, padx=20, pady=20)
  button = tk.Button(root, text="OK", command=lambda: root.destroy())
  button.pack(side="bottom", fill="none", expand=True, padx=10, pady=10)
  center(root)
  root.attributes("-topmost", True)
  root.mainloop()

showDialog()

运行此脚本时,第一个空窗口将显示在屏幕的左上角,然后完整的窗口将显示在屏幕的中央。在

我不想看到第一个空窗口(它只出现了几毫秒,但这不太好)

我怎么能做到呢?在


Tags: 脚本truehellodefrootwidthwinlabel
1条回答
网友
1楼 · 发布于 2024-09-27 20:17:09

使用以下两种方法隐藏或显示根窗口。在

def hide(root):
    root.withdraw()

def show(root):
    root.update()
    root.deiconify()

当您将根窗口的大小设为(1, 1)居中时,您应该将窗口大小设置为center method
^此处不需要{},请使用command=root.destroy。在

^{pr2}$

相关问题 更多 >

    热门问题