0

我在框架布局方面遇到问题。数据存储在数据框中,目的是能够浏览文件并从 ComboBox 中选择该文件。我正在尝试在框架中显示内容。第一次它工作正常。第二次它只是将它添加到先前数据下的垂直大小。我需要删除或替换数据。显示的数据应该是更新的数据框。我已经在 GridSizer、Vertical BoxSizer 上尝试了 .Destroy(),我尝试添加更多的尺寸器并结合 Freeze() 和 Thaw() 方法,并添加一个新的面板类。似乎没有任何效果。

我也很难让 GridSizer 中的 StaticText 框调整以适应屏幕。我究竟做错了什么?

def onOpenFile(self, event):
    """
        Create and show the Open FileDialog
    """
    global column_names
    global df_lookup_options
    global options_ID

    #self.Freeze
#Dialog 1
    dlg1 = wx.FileDialog(
        self, message="Browse Files",
        defaultFile="",
        wildcard=wildcard,
        style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR)

    if dlg1.ShowModal() == wx.ID_OK:
        paths = dlg1.GetPaths()
    
        for path in paths:
            filepath = path.replace("\\","/")
    
        xls = xlrd.open_workbook(filepath, on_demand=True)
        sheet_options = xls.sheet_names() 
        dlg1.Destroy()
#Dialog 2
        dlg2 = wx.SingleChoiceDialog(None,"Select 
Worksheet","Worksheet",sheet_options,wx.CHOICEDLG_STYLE)
        if dlg2.ShowModal() == wx.ID_OK:
            sel_sheet = dlg2.GetStringSelection()
            sel_index = dlg2.GetSelection()

        #Get Count of Worksheet Rows and Colums
        wb = xlrd.open_workbook(filepath)
        sheet = wb.sheet_by_index(sel_index)
        row_count = sheet.nrows
        column_count = sheet.ncols
        df_rows = len(df_lookup_options.index)   
        dlg2.Destroy()
#Dialog 3        
        dlg3 = wx.TextEntryDialog(None,"Enter Unique ID","Unique ID")
        if dlg3.ShowModal() == wx.ID_OK:
            unique_id = dlg3.GetValue()
        dlg3.Destroy()
#Dialog 4      
        dlg4 = wx.TextEntryDialog(None,"Enter Header Row","Header Row")
        if dlg4.ShowModal() == wx.ID_OK:
            header_row = dlg4.GetValue()
         dlg4.Destroy()
 #Dialog 5      
        dlg5 = wx.TextEntryDialog(None,"Enter First Data Column #","Start Column")
        if dlg5.ShowModal() == wx.ID_OK:
             column_start = dlg5.GetValue()
        dlg5.Destroy()
 #Add Variables to Dataframe Row
         df_lookup_options = df_lookup_options.append({'Unique 
ID':unique_id,'Worksheet':sel_sheet,'Index':sel_index,'Filepath':filepath,
                                      'Rows':row_count,'Columns':column_count,'Header 
Row':header_row,
                                      'Start Column':column_start},ignore_index=True)
        options_ID = df_lookup_options['Unique ID'].tolist()
        self.combo1.Clear()
        self.combo1.Append(options_ID)
#Get Dataframe Size
        df_row = len(df_lookup_options)
        df_col = len(df_lookup_options.columns)

#Display Source info
        #self.fgs.Destroy() 
        #self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.fgs = wx.FlexGridSizer(df_row + 1,df_col,2,25)
#Create Headers
        for i in column_names:
            self.fgs.Add(wx.StaticText(self, label = i), proportion=1, flag=wx.ALIGN_CENTER_VERTICAL 
| wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, border=5)
        self.Layout
        ##
        for r in range(df_row): 
            for y in range(df_col):
                for c in range(1,df_col + 1):
            
                    i = df_lookup_options.iloc[r, y]
                txt = str(i) 
                self.fgs.Add(wx.StaticText(self, label = txt), proportion=1, flag=wx.ALL | wx.EXPAND)               
            self.vbox.Add(self.fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=10)
        self.SetSizer(self.vbox)
        self.Fit()
        self.Layout()
        #self.Thaw

完整代码:

import wx
import xlrd
import pandas as pd
wildcard = "Excel Files(*.xlsm; *.xlsx)|*.xlsm;*.xlsx|" \
         "All files (*.*)|*.*"
#"Python source (*.py; *.pyc)|*.py;*.pyc|" \
     #"All files (*.*)|*.*"
column_names  = ['Unique ID','Worksheet','Index','Filepath','Rows','Columns','Header Row','Start 
Column']
df_lookup_options = pd.DataFrame(columns=column_names)
df_lookup = pd.DataFrame()

options_ID = []
source_selection = ""        

########################################################################
class MyForm(wx.Frame):



#----------------------------------------------------------------------
  def __init__(self):
       wx.Frame.__init__(self, None, wx.ID_ANY,
                      "Set Source Files")
    
       self.vbox = wx.BoxSizer(wx.VERTICAL)
       #self.vbox2 = wx.BoxSizer(wx.VERTICAL)
       #self.hbox = wx.BoxSizer(wx.HORIZONTAL)

       self.btn = wx.Button(self, label="Add Source File")
       self.btn.Bind(wx.EVT_BUTTON, self.onOpenFile)
       self.combo1 = wx.ComboBox(self, choices=options_ID)
       self.combo1.Bind(wx.EVT_COMBOBOX, self.onCombo)
       self.combo1.SetHint('-Choose Source-')
    
   

       self.vbox.Add(self.btn, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
       self.vbox.Add(self.combo1, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
       #self.hbox.Add(self.fgs1, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
       #self.vbox.Add(self.hbox, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
       #self.vbox.Add(self.fgs, flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=4)
    
       self.SetSizer(self.vbox)

    #----------------------------------------------------------------------

   def onOpenFile(self, event):
    """
        Create and show the Open FileDialog
    """
    global column_names
    global df_lookup_options
    global options_ID

    #self.Freeze
#Dialog 1
    dlg1 = wx.FileDialog(
        self, message="Browse Files",
        defaultFile="",
        wildcard=wildcard,
        style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR)

    if dlg1.ShowModal() == wx.ID_OK:
        paths = dlg1.GetPaths()
        
        for path in paths:
            filepath = path.replace("\\","/")
        
        xls = xlrd.open_workbook(filepath, on_demand=True)
        sheet_options = xls.sheet_names() 
        dlg1.Destroy()
#Dialog 2
        dlg2 = wx.SingleChoiceDialog(None,"Select 
Worksheet","Worksheet",sheet_options,wx.CHOICEDLG_STYLE)
        if dlg2.ShowModal() == wx.ID_OK:
            sel_sheet = dlg2.GetStringSelection()
            sel_index = dlg2.GetSelection()
    
        #Get Count of Worksheet Rows and Colums
        wb = xlrd.open_workbook(filepath)
        sheet = wb.sheet_by_index(sel_index)
        row_count = sheet.nrows
        column_count = sheet.ncols
        df_rows = len(df_lookup_options.index)   
        dlg2.Destroy()
#Dialog 3        
        dlg3 = wx.TextEntryDialog(None,"Enter Unique ID","Unique ID")
        if dlg3.ShowModal() == wx.ID_OK:
            unique_id = dlg3.GetValue()
        dlg3.Destroy()
#Dialog 4      
        dlg4 = wx.TextEntryDialog(None,"Enter Header Row","Header Row")
        if dlg4.ShowModal() == wx.ID_OK:
            header_row = dlg4.GetValue()
         dlg4.Destroy()
 #Dialog 5      
        dlg5 = wx.TextEntryDialog(None,"Enter First Data Column #","Start Column")
        if dlg5.ShowModal() == wx.ID_OK:
             column_start = dlg5.GetValue()
        dlg5.Destroy()
 #Add Variables to Dataframe Row
         df_lookup_options = df_lookup_options.append({'Unique 
ID':unique_id,'Worksheet':sel_sheet,'Index':sel_index,'Filepath':filepath,
                                      'Rows':row_count,'Columns':column_count,'Header 
Row':header_row,
                                      'Start Column':column_start},ignore_index=True)
        options_ID = df_lookup_options['Unique ID'].tolist()
        self.combo1.Clear()
        self.combo1.Append(options_ID)
#Get Dataframe Size
        df_row = len(df_lookup_options)
        df_col = len(df_lookup_options.columns)

#Display Source info
        #self.fgs.Destroy() 
        #self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.fgs = wx.FlexGridSizer(df_row + 1,df_col,2,25)
#Create Headers
        for i in column_names:
            self.fgs.Add(wx.StaticText(self, label = i), proportion=1, flag=wx.ALIGN_CENTER_VERTICAL 
| wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, border=5)
        self.Layout
        ##
        for r in range(df_row): 
            for y in range(df_col):
                for c in range(1,df_col + 1):
                
                    i = df_lookup_options.iloc[r, y]
                txt = str(i) 
                self.fgs.Add(wx.StaticText(self, label = txt), proportion=1, flag=wx.ALL | wx.EXPAND)               
            self.vbox.Add(self.fgs, proportion=1, flag=wx.ALL|wx.EXPAND, border=10)
        self.SetSizer(self.vbox)
        self.Fit()
        self.Layout()
        #self.Thaw
    def onCombo(self, event):
            global df_lookup

            self.source_selection = self.combo1.GetSelection()
            file_name = df_lookup_options.iloc[self.source_selection, 3]
            sheet_name = df_lookup_options.iloc[self.source_selection, 1]
            start_header_row = int(df_lookup_options.iloc[self.source_selection, 6]) - 1         
            start_column = int(df_lookup_options.iloc[self.source_selection, 7]) - 1        
            
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
4

0 回答 0