wx.StaticText 的标签放置在面板上而不是从函数内部更新

问题描述 投票:0回答:0

我想在选择文件后放置一个文件名。这是我的代码:

import wx

class Dlg(wx.Dialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.input_pnl = wx.Panel(self)
        self.choose_input = wx.Button(self.input_pnl, label='Select')
        self.input_filename = wx.StaticText(self.input_pnl, style=wx.ST_ELLIPSIZE_END)
        self.input_szr = wx.BoxSizer(wx.HORIZONTAL)
        self.input_szr.AddMany([
            (wx.StaticText(self.input_pnl, label='File to analyze'), 0, wx.ALIGN_CENTER),
            (self.choose_input, 0, wx.LEFT|wx.RIGHT, 7),
            (self.input_filename, 0, wx.ALIGN_CENTER)
        ])
        self.input_pnl.SetSizer(self.input_szr)

        borderSizer = wx.BoxSizer(wx.VERTICAL)
        borderSizer.AddMany([
            (self.input_pnl, 0, wx.EXPAND|wx.ALL, 10),
            (wx.StaticText(self, label='Some example text to shift the border to the right'), 0, wx.ALL, 10)])
        self.SetSizerAndFit(borderSizer)

        self.choose_input.Bind(wx.EVT_BUTTON, self.on_choose_input)
    
    def on_choose_input(self, event):
        with wx.FileDialog(self, "Open", wildcard="Plain text (*.txt)|*.txt",
                    style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            self.input_pathname = fileDialog.GetPath()
            self.input_filename.SetLabel(fileDialog.GetFilename())

if __name__ == "__main__":
    app = wx.App()
    dlg = Dlg(None)
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

但是标签根本没有更新。无需创建面板或在类的构造函数中设置标签时一切正常。

python wxpython wxwidgets
© www.soinside.com 2019 - 2024. All rights reserved.