Tkinter:使用多个实例撤回 Tkinter root window 的正确方法

Tkinter: Proper way to withdraw Tkinter root window with multiple instances

我正在使用 Pmw 创建一个漂亮的列表框。首先,我通过 Tkinter 文件对话框获取文件路径,然后对该位置的数据进行一些操作。然后我尝试显示列表框。

如果我把root和withdraw放在文件对话框行之前,列表框就不会出现。但是,如果我将它移到文件对话框请求之后,列表框就会出现,但我找不到摆脱根目录的方法 window.

import pandas as pd
import pyautogui as gui
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from pathlib import Path
import Pmw

all_file_path = filedialog.askdirectory()
raw_root = tk.Tk()
raw_root.withdraw()

---------------------
# Do some stuff
---------------------

class choiceBox( Frame ):
   def __init__( self, listItems ):
      Frame.__init__( self )
      self.pack( expand = YES, fill = BOTH )
      self.master.title( "Select" )

      self.listBox = Pmw.ScrolledListBox( self,
                                          items = listItems,
                                          listbox_height = 5,
                                          vscrollmode = "static",
                                          listbox_selectmode = EXTENDED )
      self.listBox.pack( side = LEFT, expand = YES, fill = BOTH,padx = 5, pady = 5 )

      self.copyButton = Button( self,text = ">>>", command = self.addThing )
      self.copyButton.pack( side = LEFT, padx = 5, pady = 5 )

      self.chosen = Pmw.ScrolledText( self,text_height = 6,text_width = 20 )
      self.chosen.pack( side = LEFT, expand = YES, fill = BOTH,padx = 5, pady = 5 )

   def addThing( self ):
      self.chosen.clear()
      selected = self.listBox.getcurselection()

      if selected:
         for item in selected:
            self.chosen.insert( END, item + "\n" )

names = ("A", "B", "C", "D")
choiceBox( names ).mainloop()

如有任何解释,将不胜感激。

我找到了解决方案;

raw_root.update()
raw_root.deiconify()

将 root 和 withdraw 放在文件对话框之前,然后将上面的代码放在生成列表框之前,解决了这个问题。