调用启动子窗体

时间:2017-11-10 22:40:58

标签: python wxpython

class MyDialog(wx.Frame):

    def __init__(self, parent, title):
        self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)

        wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize)

        self.panel = wx.Panel(self, size=(250, 270))
        self.combo = wx.ComboBox(self.panel, -1, pos=(60, 100))

        #call settings form here

        self.start_read_thread()
        self.Centre()
        self.Show(True)

    def read_employees(self,read_file):
        emp_list = []
        with open(read_file) as f_obj:
            for line in f_obj:
                emp_list.append(line)
        wx.CallAfter(self.combo.Append, emp_list)

    def start_read_thread(self):
        filename = 'employees.txt'
        with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
            executor.submit(self.read_employees, filename) 

app = wx.App(False)
frame = MyDialog(None, "Messaging App")
app.MainLoop()

问题:

当程序首次在用户计算机上加载时,它将打开一个提示输入电子邮件/密码的设置菜单。如果没有填写设置菜单,该程序是没有用的,因此调用设置子表单并让用户配置它是必要的。在后续的启动中,它只会从文件中读取数据。

我应该直接从__init__ constructor或其他线程调用设置子表单并传递数据吗?我还在学习线程,但对我来说,收集数据的简单子表单似乎有些过分。

之后,用户可以在设置菜单上访问相同的子表单来更新数据。

1 个答案:

答案 0 :(得分:0)

看起来有点矫枉过正 如果不确切知道你的计划是什么,我会用这样简单的事情来运行。

import wx
import os
class MyDialog(wx.Frame):

    def __init__(self, parent, title):
        self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
        wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize)
        self.panel = wx.Panel(self, size=(250, 270))
        self.emp_list = []
        e_name = 'employees.txt'
        if os.access(e_name, os.R_OK):
            with open(e_name,'r') as f_obj:
                for line in f_obj:
                    self.emp_list.append(line.strip())
        else:
            #Call employees.txt setup routine here
            frame = SetUp(parent=self)

        self.combo = wx.ComboBox(self.panel, -1,choices=self.emp_list, pos=(60, 100))
        self.Centre()
        self.Show(True)

class SetUp(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self, parent, wx.ID_ANY, "Setup employee data", size= (280,130))
        self.parent=parent
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.emp_panel = wx.Panel(self, wx.ID_ANY)
        self.employee_name = wx.TextCtrl(self.emp_panel, wx.ID_ANY, "Input Employee Name",size=(200,25))
        self.emp_button = wx.Button(self.emp_panel, wx.ID_ANY, "Add")
        self.emp_button.Bind(wx.EVT_BUTTON, self.AddEmp)
        close_button = wx.Button(self.emp_panel, wx.ID_ANY, "Close")
        close_button.Bind(wx.EVT_BUTTON, self.OnClose)
        sizer.Add(self.employee_name)
        sizer.Add(self.emp_button)
        sizer.Add(close_button)
        self.emp_panel.SetSizer(sizer)
        self.Show()

    def AddEmp(self,event):
        e_name = 'employees.txt'
        emp_name = self.employee_name.GetValue()
        with open(e_name,'a') as f_obj:
            f_obj.write(emp_name+"\n")
    def OnClose(self,event):
        e_name = 'employees.txt'
        if os.access(e_name, os.R_OK):
            with open(e_name,'r') as f_obj:
                for line in f_obj:
                    self.parent.combo.Append(line.strip())
        self.Close()

app = wx.App()
frame = MyDialog(None, "Messaging App")
app.MainLoop()

这是非常粗糙和准备好但是我有时间做其他事情 希望它会有所帮助