wxPython程序扫描

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

我试图更好地了解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次?)。如果按确定,则:

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

现在第2步不起作用。当我调试时,几乎好像PDFFrame只被扫描了一次。我的错误假设是由于app.MainLoop()持续扫描wx.App()而将其连续扫描,而wx.App()会依次调用子框架?

总是很感谢帮助/指针/加深理解,

谢谢罗布

python wxpython
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.