【问题标题】:wxPython FileDialog default filename blank under Windows 10Windows 10下wxPython FileDialog默认文件名空白
【发布时间】:2021-12-09 08:31:41
【问题描述】:

我希望在保存日志文件时有一个基于时间的文件名。当我尝试设置默认文件名时,对话框出现一个空白文件名,如下所示。我已经尝试了等效的位置函数调用,它也不起作用。

知道如何让 wx.FileDialog() 设置文件名,以便您只需单击“保存”以使用默认名称保存文件吗?

在 Windows 10 下使用以下版本:

Python 3.8.6(tags/v3.8.6:db45529,2020 年 9 月 23 日,15:52:53)[MSC v.1927 64 位 (AMD64)]

wx.version: 4.1.1 msw (phoenix) wxWidgets 3.1.5

不起作用的代码:

def OnSave(self, event):
    default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
    dlg = wx.FileDialog(self.frame, message = "Save Log Contents",
                                    defaultDir = os.getcwd(),
                                    defaultFile = default_file,
                                    wildcard = "Log files (*.log)|*.log",
                                    style = wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
    if dlg.ShowModal() == wx.ID_CANCEL:
        dlg.Destroy()
        return

    file_path = dlg.GetPath()
    self.window.tc.AppendText("%s  Saving log to %s\n" % (datetime.datetime.now(), file_path))
    self.window.tc.SaveFile(file_path)
    dlg.Destroy()
    return True

【问题讨论】:

  • 确保您运行的是最新的源代码。如果源代码未保存或调试运行错误的副本,有时会再次出现遗留错误。
  • 文件名中不能有:!重新格式化您的默认文件名。

标签: wxpython


【解决方案1】:

遗憾的是,我所能做的就是确认在 Linux wx 4.1.1 下它可以按预期工作。

import wx
import datetime

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.select_button = wx.Button(panel, label="Select files")
        self.convert_and_merge_button = wx.Button(panel, label="Merge files")
        sizer.Add(self.select_button, 0, 0, 0)
        sizer.Add(self.convert_and_merge_button, 0, 0, 0)
        self.select_button.Bind(wx.EVT_BUTTON, self.pick_files)
        self.convert_and_merge_button.Bind(wx.EVT_BUTTON, self.convert_and_merge)
        self.load_options = "Pdf and Image Files |*.pdf;*.gif;*.bmp;*.tif;*.png;"
        self.save_options = "Pdf Files |*.pdf;*.log;"
        self.convert_and_merge_button.Enable(False)
        panel.SetSizer(sizer)

    def pick_files(self, event):
        with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
            if fileDialog.ShowModal() != wx.ID_CANCEL:
                self.files_list = fileDialog.GetPaths()
                self.convert_and_merge_button.Enable()

    def convert_and_merge(self, event):
        default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
        print (default_file)
        with wx.FileDialog(self, "Convert and merge", wildcard=self.save_options,
                           defaultDir = "/home/rolf",
                           defaultFile=default_file,
                           style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:

            if fileDialog.ShowModal() != wx.ID_CANCEL:
                # pass parameters to Converter class
                merge_file = fileDialog.GetPath()
                #Test that the output file is not in the input list
                if merge_file in self.files_list:
                    wx.MessageBox('The chosen output file is in the input files\n Choose another file', 'Error', wx.OK | wx.ICON_INFORMATION)
                    return
                self.converter(self.files_list, merge_file)

    def converter(self, files_list, merge_file):
        print ("Merging:\n"+str(files_list)+"\n into\n"+str(merge_file))

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'A test dialog')
        frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()

【讨论】:

    【解决方案2】:

    您的文件名中不能包含:。更改此行:

      default_file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ".log"
    

    到这里:

      default_file = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S') + ".log"
    

    你会得到更好的结果。

    我是如何发现这一点的(不是立即发现的):

    直到我尝试使用设置默认文件名时才发现这一点

    dlg.SetFilename(default_file)
    

    在打开对话框之前,我收到了一条错误消息。然后我将默认文件名更改为“junk.log”,它工作正常。然后我将 default_file 打印到控制台并发现了问题。

    【讨论】:

    • 很棘手,我认为这是一个仅限 Windows 的怪癖。
    • 我永远不会发现。很棒的收获!
    • @RolfofSaxony 我猜 *nix 系统文件名中允许使用冒号。在 Windows 中,冒号通常将驱动器号与路径的其余部分分开。奇怪的是,在这种情况下,它不会从构造函数或调用中抛出任何错误或异常(但如果你直接调用 SetFilename 方法,我尝试过,但它会出现错误。)
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多