【问题标题】:wxPython Program ScanwxPython程序扫描
【发布时间】:2019-11-18 15:16:32
【问题描述】:

我试图更好地理解 wxPython 如何“扫描”。

请看我下面的代码:

import os
import wx
from time import sleep

NoFilesToCombine = 0

class PDFFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(400,400))
        panel = wx.Panel(self)
        self.Show()

        try:                        #Set values of PDFNoConfirmed to zero on 1st initialisation
            if PDFNoConfimed != 0:
                None
        except UnboundLocalError:     
            PDFNoConfimed = 0   
        try:                        #Set values of PDFNoConfirmed to zero on 1st initialisation
            if PDFNoConfirmedInitially != 0:
                None
        except UnboundLocalError:     
            PDFNoConfirmedInitially = 0   

        while ((PDFNoConfimed == 0) and (PDFNoConfirmedInitially == 0)):
            while PDFNoConfirmedInitially == 0:
                BoxInputNo = wx.NumberEntryDialog(panel, "So You Want To Combine PDF Files?", "How Many?", "Please Enter", 0, 2, 20)
                if BoxInputNo.ShowModal() == wx.ID_OK: #NumberEntryDialog Pressed OK
                    NoFilesToCombine = BoxInputNo.GetValue()
                    PDFNoConfirmedInitially = 1
                elif BoxInputNo.ShowModal() == wx.ID_CANCEL:
                    exit()
            print(NoFilesToCombine)
            ConfirmationLabel = wx.StaticText(panel, -1, label="You Have Selected " + str(NoFilesToCombine) + " Files To Combine, Is This Right?", pos=(20, 100))
            ConfirmationBoxConfirm = wx.ToggleButton(panel, label="Confirm", pos=(20, 200))
            ConfirmationBoxCancel = wx.ToggleButton(panel, label="Cancel", pos=(180, 200))
            #if ConfirmationBoxConfirm.GetValue() == 1:
            #    exit()
            if ConfirmationBoxCancel.GetValue() == 1:
                PDFNoConfirmedInitially = 0

app = wx.App()
frame = PDFFrame(None, title="Robs PDF Combiner Application")
app.MainLoop()

现在这是一项正在进行的工作,因此显然还没有完成。但是,我想通过上述方式完成的是:

  1. 显示一个数字输入弹出窗口。如果用户按“取消”退出应用程序(这可行,但由于某种原因需要按 2 次?)。如果按 OK,则:

  2. 显示在步骤 1 中输入的数字,带有 2 个附加按钮。 “确认”尚未执行任何操作,但“取消”应将您带回第 1 步。(通过重置 PDFNoConfirmedInitially 标志)。

现在第 2 步不起作用。当我调试它几乎看起来好像PDFFrameonly 被扫描一次。我可能错误的假设是,由于app.MainLoop() 不断扫描 wx.App() 而这又会调用子框架,这将被不断扫描?

帮助/指点/更深层次的理解总是很感激,

谢谢 抢

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    1) ShowModal() 显示对话窗口,你使用它两次

    if BoxInputNo.ShowModal() == wx.ID_OK:
    

    elif BoxInputNo.ShowModal() == wx.ID_CANCEL:
    

    所以它会显示你的窗口两次。

    只有在第二次检查wx.ID_CANCEL

    你应该只运行一次并检查它的结果

    result = BoxInputNo.ShowModal()
    
    if result == wx.ID_OK:
        pass
    elif result == wx.ID_CANCEL:
        self.Close()
        return
    

    2) 您必须为按钮分配功能,此功能应重置变量并再次显示对话框窗口。但我认为wx.Button 可能比wx.ToggleButton 更好

        ConfirmationBoxCancel = wx.Button(panel, label="Cancel", pos=(180, 200))
        ConfirmationBoxCancel.Bind(wx.EVT_BUTTON, self.on_button_cancel)
    
    def on_button_cancel(self, event):
        #print('event:', event)
        pass
        # reset variable and show dialog window
    

    坦率地说,我不了解您的一些变量。也许如果您使用True/False 而不是0/1,那么它们将更具可读性。但对我来说主要问题是while 循环。 GUI 框架(wxPython、tkinter、PyQt 等)应该只运行一个循环 - Mainloop()。任何其他循环都可能阻塞Mainloop(),GUI 将冻结。

    我创建了自己的版本,没有任何while 循环,但我不知道它是否能解决所有问题

    import wx
    
    class PDFFrame(wx.Frame):
    
        def __init__(self, parent, title):
            super().__init__(parent, -1, title, size=(400,400))
    
            self.panel = wx.Panel(self)
            self.Show()
    
            # show dialog at start 
            if self.show_dialog(self.panel):
                # create StaticLabel and buttons
                self.show_confirmation(self.panel)
            else:
                # close main window and program
                self.Close()
    
    
        def show_dialog(self, panel):
            """show dialog window"""
    
            global no_files_to_combine
    
            box_input_no = wx.NumberEntryDialog(panel, "So You Want To Combine PDF Files?", "How Many?", "Please Enter", 0, 2, 20)
    
            result = box_input_no.ShowModal()
    
            if result == wx.ID_OK: #NumberEntryDialog Pressed OK
                no_files_to_combine = box_input_no.GetValue()
                return True
            elif result == wx.ID_CANCEL:
                print('exit')
                return False
    
    
        def show_confirmation(self, panel):
            """create StaticLabel and buttons"""
    
            self.confirmation_label = wx.StaticText(self.panel, -1, label="You Have Selected {} Files To Combine, Is This Right?".format(no_files_to_combine), pos=(20, 100))
    
            self.confirmation_box_confirm = wx.Button(self.panel, label="Confirm", pos=(20, 200))
            self.confirmation_box_cancel = wx.Button(self.panel, label="Cancel", pos=(180, 200))
            # assign function
            self.confirmation_box_confirm.Bind(wx.EVT_BUTTON, self.on_button_confirm)
            self.confirmation_box_cancel.Bind(wx.EVT_BUTTON, self.on_button_cancel)
    
    
        def update_confirmation(self):
            """update existing StaticLabel"""
    
            self.confirmation_label.SetLabel("You Have Selected {} Files To Combine, Is This Right?".format(no_files_to_combine))
    
    
        def on_button_cancel(self, event):
            """run when pressed `Cancel` button"""
    
            #print('event:', event)
    
            # without `GetValue()`        
            if self.show_dialog(self.panel):
                # update existing StaticLabel
                self.update_confirmation()
            else:
                # close main window and program
                self.Close()
    
    
        def on_button_confirm(self, event):
            """run when pressed `Confirn` button"""
    
            #print('event:', event)
    
            # close main window and program
            self.Close()
    
    
    # --- main --- 
    
    no_files_to_combine = 0
    
    app = wx.App()
    frame = PDFFrame(None, title="Robs PDF Combiner Application")
    app.MainLoop()
    

    【讨论】:

    • 非常感谢您花时间解决我的问题并如此彻底地回答。提供了很大的帮助,我非常感谢
    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 2011-04-06
    相关资源
    最近更新 更多