python - 如何创建一个循环模块,为每个 SQL 行结果一次创建一个窗口?

标签 python mysql python-3.x wxpython

我正在创建一个 IP 配置程序,在其中一部分,我必须修改数据库中的选定行以将信息修改到它们中。

为了让我做到这一点,我必须在每一行创建一个窗口,然后在用户按下下一步之前等待用户完成输入,然后会弹出一个新窗口,旧窗口将被销毁.

这是我为模块准备的代码:

class moddtb(wx.Frame):
    def __init__(self, title, parent=None):
        global l_a
        t = len(l_a)
        self.n = 0
        self.b = 0
        while self.n<t:
            while True:
                wx.Frame.__init__(self, parent=parent, title=title, size=(1100, 500))
                panel = wx.Panel(self)
                self.Centre(direction=wx.BOTH)
                self.Bind(wx.EVT_CLOSE, self.cls)
                print(self.n)
                mycursor.execute("SELECT * FROM ip_config WHERE id=%s;", (l_a[self.n],))
                new_ip = mycursor.fetchone()
                self.n_id = str(new_ip[0])
                self.n_hn = str(new_ip[1])
                self.n_rl = str(new_ip[2])
                self.n_aut = str(new_ip[3])
                self.n_ali = str(new_ip[4])
                self.n_typ = str(new_ip[5])
                self.n_env = str(new_ip[6])
                self.n_isc = str(new_ip[7])
                self.n_sta = str(new_ip[8])
                self.n_vla = str(new_ip[9])
                self.n_ip = str(new_ip[10])
                self.n_pui = str(new_ip[11])
                self.n_gri = str(new_ip[12])
                self.n_idr = str(new_ip[13])
                self.n_sys = str(new_ip[14])
                self.n_aps = str(new_ip[15])
                self.n_das = str(new_ip[16])
                self.n_phs = str(new_ip[17])
                self.n_rac = str(new_ip[18])
                self.n_lom = str(new_ip[19])
                self.n_phm = str(new_ip[20])
                self.n_mod = str(new_ip[21])
                self.n_ser = str(new_ip[22])
                self.n_prv = str(new_ip[23])
                self.n_os = str(new_ip[24])
                self.n_war = str(new_ip[25])
                self.n_es = str(new_ip[26])
                self.n_el = str(new_ip[27])
                if self.n_hn is None:
                    wx.MessageBox('An error has been made, selected row %s is empty. Skipping to next row.' % (self.n_id), 'Creation Error', wx.OK | wx.ICON_ERROR)             
                    continue
                f1 = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, "Arial")
                f2 = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD, False, "Sergoe UI")

                ttllbl = TransparentText(panel, id=wx.ID_ANY, label="Please fill out the information in this form:   ", pos=(60,27),
                                         size=(200,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                ttllbl.SetFont(f2)
                idlbl = TransparentText(panel, id=wx.ID_ANY, label="ID: ", pos=(60,77),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                idlbl.SetFont(f1)
                idtbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_id, pos=(150, 77), size=(180,25), style=wx.TE_READONLY)
                hnlbl = TransparentText(panel, id=wx.ID_ANY, label="Host Name: ", pos=(60,107),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                hnlbl.SetFont(f1)
                self.hntbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_hn, pos=(150, 107), size=(180,25))
                rolelbl = TransparentText(panel, id=wx.ID_ANY, label="Role: ", pos=(60,137),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                rolelbl.SetFont(f1)
                self.roletbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_rl, pos=(150, 137), size=(180,25))
                athlbl = TransparentText(panel, id=wx.ID_ANY, label="Authorize: ", pos=(60,167),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                athlbl.SetFont(f1)
                self.athtbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_aut, pos=(150, 167), size=(180,25))
                alslbl = TransparentText(panel, id=wx.ID_ANY, label="Alias: ", pos=(60,197),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                alslbl.SetFont(f1)
                self.alstbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_ali, pos=(150, 197), size=(180,25))
                typlbl = TransparentText(panel, id=wx.ID_ANY, label="Type: ", pos=(60,227),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                typlbl.SetFont(f1)
                self.typtbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_typ, pos=(150, 227), size=(180,25))
                envlbl = TransparentText(panel, id=wx.ID_ANY, label="Enviroment: ", pos=(60,257),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                envlbl.SetFont(f1)
                self.envtbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_env, pos=(150, 257), size=(180,25))
                isiplbl = TransparentText(panel, id=wx.ID_ANY, label="ISCI IP: ", pos=(60,287),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                isiplbl.SetFont(f1)
                self.isiptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_isc, pos=(150, 287), size=(180,25))
                staiplbl = TransparentText(panel, id=wx.ID_ANY, label="Static IP: ", pos=(60,317),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                staiplbl.SetFont(f1)
                self.staiptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_sta, pos=(150, 317), size=(180,25))
                vliplbl = TransparentText(panel, id=wx.ID_ANY, label="VLAN: ", pos=(60,347),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                vliplbl.SetFont(f1)
                self.vliptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_vla, pos=(150, 347), size=(180,25))

                maniplbl = TransparentText(panel, id=wx.ID_ANY, label="Management IP: ", pos=(360,77),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                maniplbl.SetFont(f1)
                self.maniptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_ip, pos=(500, 77), size=(180,25), style=wx.TE_READONLY)
                pubiplbl = TransparentText(panel, id=wx.ID_ANY, label="Public IP: ", pos=(360,107),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                pubiplbl.SetFont(f1)
                self.pubiptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_pui, pos=(500, 107), size=(180,25))
                grpiplbl = TransparentText(panel, id=wx.ID_ANY, label="Group IP: ", pos=(360,137),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                grpiplbl.SetFont(f1)
                self.grpiptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_gri, pos=(500, 137), size=(180,25))
                idriplbl = TransparentText(panel, id=wx.ID_ANY, label="IDRAC IP: ", pos=(360,167),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                idriplbl.SetFont(f1)
                self.idriptbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_idr, pos=(500, 167), size=(180,25))
                sysszlbl = TransparentText(panel, id=wx.ID_ANY, label="System Size: ", pos=(360,197),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                sysszlbl.SetFont(f1)
                self.syssztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_sys, pos=(500, 197), size=(180,25))
                appszlbl = TransparentText(panel, id=wx.ID_ANY, label="Application Size: ", pos=(360,227),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                appszlbl.SetFont(f1)
                self.appsztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_aps, pos=(500, 227), size=(180,25))
                dtszlbl = TransparentText(panel, id=wx.ID_ANY, label="Data Size: ", pos=(360,257),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                dtszlbl.SetFont(f1)
                self.dtsztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_das, pos=(500, 257), size=(180,25))
                physzlbl = TransparentText(panel, id=wx.ID_ANY, label="Physical Size: ", pos=(360,287),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                physzlbl.SetFont(f1)
                self.physztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_phs, pos=(500, 287), size=(180,25))
                rdconlbl = TransparentText(panel, id=wx.ID_ANY, label="Raid Config: ", pos=(360,317),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                rdconlbl.SetFont(f1)
                self.rdcontbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_rac, pos=(500, 317), size=(180,25))
                lgmemszlbl = TransparentText(panel, id=wx.ID_ANY, label="Logical Memory \nSize: ", pos=(360,347),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                lgmemszlbl.SetFont(f1)
                self.lgmemsztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_lom, pos=(500, 347), size=(180,25))

                phymemszlbl = TransparentText(panel, id=wx.ID_ANY, label="Physical Memory \nSize: ", pos=(700,77),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                phymemszlbl.SetFont(f1)
                self.phymemsztbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_phm, pos=(840, 77), size=(180,25))
                mdllbl = TransparentText(panel, id=wx.ID_ANY, label="Model: ", pos=(700,117),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                mdllbl.SetFont(f1)
                self.mdltbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_mod, pos=(840, 117), size=(180,25))
                srlnumlbl = TransparentText(panel, id=wx.ID_ANY, label="Serial Number: ", pos=(700,147),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                srlnumlbl.SetFont(f1)
                self.srlnumtbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_ser, pos=(840, 147), size=(180,25))
                prvcpulbl = TransparentText(panel, id=wx.ID_ANY, label="Processor/VCPU: ", pos=(700,177),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                prvcpulbl.SetFont(f1)
                self.prvcputbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_prv, pos=(840, 177), size=(180,25))
                oslbl = TransparentText(panel, id=wx.ID_ANY, label="OS: ", pos=(700,207),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                oslbl.SetFont(f1)
                self.ostbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_os, pos=(840, 207), size=(180,25))
                wrtylbl = TransparentText(panel, id=wx.ID_ANY, label="Warranty: ", pos=(700,237),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                wrtylbl.SetFont(f1)
                self.wrtytbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_war, pos=(840, 237), size=(180,25))
                eoslbl = TransparentText(panel, id=wx.ID_ANY, label="End of Support: ", pos=(700,267),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                eoslbl.SetFont(f1)
                self.eostbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_es, pos=(840, 267), size=(180,25))
                eollbl = TransparentText(panel, id=wx.ID_ANY, label="End of Life: ", pos=(700,297),
                                         size=(50,50), style=wx.TRANSPARENT_WINDOW, name='transparenttext')
                eollbl.SetFont(f1)
                self.eoltbx = wx.TextCtrl(panel, id=wx.ID_ANY, value=self.n_el, pos=(840, 297), size=(180,25))
                if self.b==1:
                    b3a = wx.Bitmap("button3 a.png")
                    b3b = wx.Bitmap("button3 b.png")
                    b4a = wx.Bitmap("button4 a.png")
                    b4b = wx.Bitmap("button4 b.png")
                    dtbbtnc = btn.GenBitmapButton(panel, wx.ID_ANY, bitmap=b3a, pos=(700,330),  style=wx.NO_BORDER|wx.BU_EXACTFIT,size=(147, 62))
                    dtbbtnc.SetBitmapSelected(b3b)
                    dtbbtnc.Bind(wx.EVT_BUTTON, self.cc)
                    dtbbtnd = btn.GenBitmapButton(panel, wx.ID_ANY, bitmap=b4a, pos=(880,330),  style=wx.NO_BORDER|wx.BU_EXACTFIT,size=(147, 62))
                    dtbbtnd.SetBitmapSelected(b4b)
                    dtbbtnd.Bind(wx.EVT_BUTTON, self.cls2)
                    self.Show()
                else:
                    b8a = wx.Bitmap("button8 a.png")
                    b8b = wx.Bitmap("button8 b.png")
                    b4a = wx.Bitmap("button4 a.png")
                    b4b = wx.Bitmap("button4 b.png")
                    dtbbtnc = btn.GenBitmapButton(panel, wx.ID_ANY, bitmap=b8a, pos=(880,330),  style=wx.NO_BORDER|wx.BU_EXACTFIT,size=(147, 62))
                    dtbbtnc.SetBitmapSelected(b8b)
                    dtbbtnc.Bind(wx.EVT_BUTTON, self.cc)
                    dtbbtnd = btn.GenBitmapButton(panel, wx.ID_ANY, bitmap=b4a, pos=(700,330),  style=wx.NO_BORDER|wx.BU_EXACTFIT,size=(147, 62))
                    dtbbtnd.SetBitmapSelected(b4b)
                    dtbbtnd.Bind(wx.EVT_BUTTON, self.cls)
                    self.Show()
                if t-1==self.n or t==1:
                    self.b=1
                if moddtb.IsShown(self)==False:
                    break               
            self.n+=1
    def cls2(self, event):
        frame = dspdtb()
        self.Destroy()  
    def cls(self, event):
        nf = MainPage()
        nf.Show()
        self.Destroy()
    def cc(self, event):
        cr_a = self.hntbx.GetValue()
        print ("Value: \"",cr_a,"\"")
        if cr_a != None or cr_a !="" or cr_a !=" " or cr_a !="  " :
            id = self.n_id
            ip = self.n_ip
            cr_b = self.roletbx.GetValue()
            cr_c = self.athtbx.GetValue()
            cr_d = self.alstbx.GetValue()
            cr_e = self.typtbx.GetValue()
            cr_f = self.envtbx.GetValue()
            cr_g = self.isiptbx.GetValue()
            cr_h = self.staiptbx.GetValue()
            cr_i = self.vliptbx.GetValue()
            cr_j = self.pubiptbx.GetValue()
            cr_k = self.grpiptbx.GetValue()
            cr_l = self.idriptbx.GetValue()
            cr_m = self.syssztbx.GetValue()
            cr_n = self.appsztbx.GetValue()
            cr_o = self.dtsztbx.GetValue()
            cr_p = self.physztbx.GetValue()
            cr_q = self.rdcontbx.GetValue()
            cr_r = self.lgmemsztbx.GetValue()
            cr_s = self.phymemsztbx.GetValue()
            cr_t = self.mdltbx.GetValue()
            cr_u = self.srlnumtbx.GetValue()
            cr_v = self.prvcputbx.GetValue()
            cr_w = self.ostbx.GetValue()
            cr_x = self.wrtytbx.GetValue()
            cr_y = self.eostbx.GetValue()
            cr_z = self.eoltbx.GetValue()
            mycursor.execute("UPDATE ip_config SET host_name=%s, role=%s, authorize=%s, alias=%s, type=%s, enviroment=%s, isci_ip=%s, static_ip=%s, vlan=%s, public_ip=%s, group_ip=%s, idrac_ip=%s, system_size=%s, application_size=%s, data_size=%s, physical_size=%s, raid_config=%s, logical_mem=%s, physical_mem=%s, model=%s, serial_number=%s, `processor/vcpu`=%s, os=%s, warranty=%s, end_of_support=%s, end_of_life=%s WHERE id=%s;", (cr_a, cr_b, cr_c, cr_d, cr_e, cr_f, cr_g, cr_h, cr_i, cr_j, cr_k, cr_l, cr_m, cr_n, cr_o, cr_p, cr_q, cr_r, cr_s, cr_t, cr_u, cr_v, cr_w, cr_x, cr_y, cr_z, id))
            if self.b==1:
                LoadThread()
                loada = load()
                loada.ShowModal()
                wx.MessageBox('A new connection has been created, ', 'Connection Created', wx.OK)
            frame = MainPage()
            self.Destroy()      
        else:
            wx.MessageBox('An error has been made, please fill out the host name before you continue...', 'Creation Error', wx.OK | wx.ICON_ERROR)
            frame = MainPage()
            self.Destroy()      

但是这个模块的问题有两点:

  1. 如果我运行这个程序,就会发生无限循环,导致打开的窗口永无止境地循环。
  2. 即使没有奇怪的无限循环问题,如果我只考虑第一个循环来修复它,那么它只会一次显示所有窗口,而不是一个一个地显示它们。在计划中制造困惑。

有没有办法让每行一个一个地显示修改窗口,而不必一次显示所有修改窗口?

感谢任何帮助。提前致谢。

最佳答案

一种实现您想要的方法是将每个配置窗口显示为模态窗口,就像您在搜索文件 (wx.FileDialog) 时获得的窗口一样。

请参阅下面的代码(和注释)以了解执行此操作的一种方法。

代码的问题在于它只能在 Windows 中完美运行。在 macOS 和 Linux 中,主窗口并未完全禁用。例如,在 macOS 中,您无法最小化和最大化主窗口,但您可以与按钮进行交互;在 Linux 中,您无法与按钮交互,但可以最小化和最大化窗口。因此,根据您要支持的平台,您需要稍微调整一下代码。

import wx

class MyFrame(wx.Frame):
    """
    Main Window of the App
    """
    def __init__(self):
        title='Multiple consecutive windows (ShowModal way)'
        super().__init__(None, title=title)

        #### Variables
        # Number of configuration windows that will be created
        self.WinNum = 5
        #### Widgets
        self.panel = wx.Panel(self)
        self.buttonShow = wx.Button(self.panel, pos=(50, 50), label='ShowModal')
        self.buttonTest = wx.Button(self.panel, pos=(50, 100), label='Test')
        #### Bind
        self.buttonShow.Bind(wx.EVT_BUTTON, self.ShowWindows)
        #### Position of the window
        self.SetPosition(pt=(50, 50))

    def ShowWindows(self, event):
        """
        This will create the configuration windows as modal windows. 
        """
        i = 0
        # This is to show a way to pass information from the configuration
        # windows to the main window. Just in case.
        self.configResults = []

        #### Depending of the OS you want to support you will need to modify the
        #### this window as explained in the answer. For macOS at least the 
        #### ShowModal button must be disable to avoid launching parallel 
        #### configuration windows. Remove the comment in the line below
        #self.buttonShow.Disable()

        while i < self.WinNum:
            a = WinModal(i)
            a.ShowModal()
            i += 1

        #### After all windows are closed the button must be enable again in
        #### macOS. Remove the comment in the line below.
        #self.buttonShow.Enable()

        #### Check that the information was passed 
        print(self.configResults)

class WinModal(wx.Frame):
    """
    Configuration window
    """ 
    def __init__(self, ThisWinNum):
        title = 'This is window number: ' + str(ThisWinNum)
        super().__init__(None, title=title)
        #### Variables
        self.ThisWinNum = ThisWinNum
        # To append Cancel when closing the configuration window without 
        # clicking Ok
        self.cancel = True
        #### Widgets
        self.panel = wx.Panel(self)
        self.buttonOk = wx.Button(self.panel, pos=(50, 50), label='Ok')
        #### Positions
        self.SetPosition(pt=(200, 200))
        #### Bind
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Bind(wx.EVT_BUTTON, self.onOk)

    def ShowModal(self):
        """
        This function is the one giving the wx.FileDialog behavior
        """
        self._disabler = wx.WindowDisabler(self)
        self.Show()
        self.eventLoop = wx.GUIEventLoop()
        self.eventLoop.Run()

    def OnClose(self, event):
        """
        To handle closing the windows because you need to exit the eventLoop
        of the modal window.
        """
        if self.cancel:
            frame.configResults.append('Cancel')
        else:
            pass

        del self._disabler
        self.eventLoop.Exit()
        self.Destroy()

    def onOk(self, event):
        """ 
        Do your configuration and exit by calling self.OnClose. You will need 
        something similar if you place a Cancel button in the configuration 
        windows.
        """
        print(self.ThisWinNum)
        # Pass the informartion to the main window
        frame.configResults.append(self.ThisWinNum)
        # To avoid append when Ok is clicked
        self.cancel = False
        self.OnClose(event)

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
else:
    pass

关于python - 如何创建一个循环模块,为每个 SQL 行结果一次创建一个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56285657/

相关文章:

mysql - 选择项目,然后选择 parent 的名字

python - 如何将对象存储在动态生成的变量中?

python-3.x - 命名空间前缀无效

python - 将对象附加到列表时删除引号

python - Dask "Column Assignment Doesn' t 支持时间戳”

mysql - 在不存在的地方插入——无主键

python - 有没有办法将列表理解重写为 for 循环?

python - 删除矩阵中包含某些元素的行(python)

python - 逗号分隔列表的 argparse 操作或类型

python - 如何将Python字典中的数据插入MySQL?