【问题标题】:Tkinter: Proper way to withdraw Tkinter root window with multiple instancesTkinter:使用多个实例撤回 Tkinter 根窗口的正确方法
【发布时间】:2019-11-19 10:20:17
【问题描述】:

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

如果我在文件对话框行之前放置根并退出,列表框将不会出现。但是,如果我将它移到文件对话框请求之后,会出现列表框,但我找不到摆脱根窗口的方法。

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()

这里的任何解释都将不胜感激。

【问题讨论】:

    标签: python tkinter listbox pmw


    【解决方案1】:

    我找到了解决办法;

    raw_root.update()
    raw_root.deiconify()
    

    将root和withdraw放在filedialog之前,然后将上面的代码放在listbox生成之前,可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2013-10-21
      • 2018-05-29
      • 1970-01-01
      相关资源
      最近更新 更多