我正在尝试打印到wxpython中的特定选项卡式面板,但我的下面代码似乎打印到第3个(正在运行的作业)TAB面板窗口,我无法解决原因。我想打印第二个(QueueList)TAB面板。
import wx
import sys
global queueList
queueList = []
class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
#global panel1
wx.Frame.__init__(self, parent, id, title, size=(800, 700))
self.tabbed = wx.Notebook(self, -1, style=(wx.NB_TOP))
self.panel1 = wx.Panel(self.tabbed, -1)
self.panel2 = wx.Panel(self.tabbed, -1)
self.panel3 = wx.Panel(self.tabbed, -1)
self.tabbed.AddPage(self.panel1, "Submit Job")
self.tabbed.AddPage(self.panel2, "Queue")
self.tabbed.AddPage(self.panel3, "Running Jobs")
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.SetMenuBar(menuBar)
self.Centre()
self.submit(self)
self.queue(self)
self.running(self)
def submit(self, event):
self.Show()
dt1 = MyFileDropTarget(self)
self.tc_files = wx.TextCtrl(self.panel1, wx.ID_ANY, pos=(42, 120), size=(500, 25))
self.tc_files.SetDropTarget(dt1)
self.buttonGo = wx.Button(self.panel1, -1, "Submit", pos=(90,530))
self.buttonGo.Bind(wx.EVT_BUTTON, self.submit1)
self.buttonClose = wx.Button(self.panel1, -1, "Quit", pos=(195,530))
self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)
outputtxt3 = '''Drag & Drop Folder of Packages to Verify'''
wx.StaticText(self.panel1, -1, outputtxt3, (33, 64), style=wx.ALIGN_CENTRE)
def notify(self, indir):
"""Update file in testcontrol after drag and drop"""
self.tc_files.SetValue(indir[0])
global indirTemp
indirTemp = indir
def submit1(self, edit):
list1 = '\n'.join(indirTemp)
queueList.append(list1)
print queueList
wx.MessageBox('Job Submitted')
def queue(self, event):
self.Show()
self.buttonClose2 = wx.Button(self.panel2, -1, "Quit", pos=(195,170))
self.buttonClose2.Bind(wx.EVT_BUTTON, self.OnClose)
global log2
log2 = wx.TextCtrl(self.panel2, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
self.redir2=RedirectText(log2)
sys.stdout=self.redir2
def showQueue(self, edit):
global queueList
print queueList
def running(self, event):
self.Show()
self.buttonClose3 = wx.Button(self.panel3, -1, "Quit", pos=(195,170))
self.buttonClose3.Bind(wx.EVT_BUTTON, self.OnClose)
global log3
log3 = wx.TextCtrl(self.panel3, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
self.redir3=RedirectText(log3)
sys.stdout=self.redir3
def go3(self, edit):
print "do something"
def OnClose(self, e):
self.Close(True)
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.notify(filenames)
class RedirectText:
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl
def write(self,string):
self.out.WriteText(string)
app = wx.App()
ScrolledWindow(None, -1, 'Application')
app.MainLoop()
答案 0 :(得分:1)
原因是您在running()
中致电queue()
后致电__init__()
,这是在您进行打印之前重定向stdout的最后一件事。
这是你的代码的一个版本,当你运行它时,通过输出它正在做的事情的痕迹来使它显而易见......
import wx
import sys
global queueList
queueList = []
class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
#global panel1
wx.Frame.__init__(self, parent, id, title, size=(800, 700))
self.tabbed = wx.Notebook(self, -1, style=(wx.NB_TOP))
self.panel1 = wx.Panel(self.tabbed, -1)
self.panel2 = wx.Panel(self.tabbed, -1)
self.panel3 = wx.Panel(self.tabbed, -1)
self.tabbed.AddPage(self.panel1, "Submit Job")
self.tabbed.AddPage(self.panel2, "Queue")
self.tabbed.AddPage(self.panel3, "Running Jobs")
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.SetMenuBar(menuBar)
self.Centre()
self.submit(self)
self.queue(self)
self.running(self)
def submit(self, event):
self.Show()
dt1 = MyFileDropTarget(self)
self.tc_files = wx.TextCtrl(self.panel1, wx.ID_ANY, pos=(42, 120), size=(500, 25))
self.tc_files.SetDropTarget(dt1)
self.buttonGo = wx.Button(self.panel1, -1, "Submit", pos=(90,530))
self.buttonGo.Bind(wx.EVT_BUTTON, self.submit1)
self.buttonClose = wx.Button(self.panel1, -1, "Quit", pos=(195,530))
self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)
outputtxt3 = '''Drag & Drop Folder of Packages to Verify'''
wx.StaticText(self.panel1, -1, outputtxt3, (33, 64), style=wx.ALIGN_CENTRE)
def notify(self, indir):
"""Update file in testcontrol after drag and drop"""
self.tc_files.SetValue(indir[0])
global indirTemp
indirTemp = indir
def submit1(self, edit):
list1 = '\n'.join(indirTemp)
queueList.append(list1)
sys.stderr.write("submit1 printing\n")
print queueList
wx.MessageBox('Job Submitted')
def queue(self, event):
self.Show()
self.buttonClose2 = wx.Button(self.panel2, -1, "Quit", pos=(195,170))
self.buttonClose2.Bind(wx.EVT_BUTTON, self.OnClose)
global log2
log2 = wx.TextCtrl(self.panel2, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
sys.stderr.write("redirecting stdout to log2\n")
self.redir2=RedirectText(log2)
sys.stdout=self.redir2
def showQueue(self, edit):
sys.stderr.write("showQueue printing\n")
global queueList
print queueList
def running(self, event):
self.Show()
self.buttonClose3 = wx.Button(self.panel3, -1, "Quit", pos=(195,170))
self.buttonClose3.Bind(wx.EVT_BUTTON, self.OnClose)
global log3
log3 = wx.TextCtrl(self.panel3, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
sys.stderr.write("redirecting stdout to log3\n")
self.redir3=RedirectText(log3)
sys.stdout=self.redir3
def go3(self, edit):
sys.stderr.write("go3 printing\n")
print "do something"
def OnClose(self, e):
self.Close(True)
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.notify(filenames)
class RedirectText:
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl
def write(self,string):
self.out.WriteText(string)
app = wx.App()
ScrolledWindow(None, -1, 'Application')
app.MainLoop()
注意你必须去做一些简单调试的麻烦,因为你已经劫持了stdout。一旦您对此实验感到满意,我仍强烈建议您在实际完成申请时找到更好的方法。它不像你那样坚固或可维护。对此的其他证据是对全局变量的需求:一个很大的警告信号表明您的代码将变得脆弱且难以维护。
答案 1 :(得分:1)
正如我在其他答案中所提到的,它不起作用的表面原因是因为有一个错误:) IE所写的逻辑并不是预期的。更深层次的原因是因为应用需要一些更好的设计。 Appended是一个如何完成的例子。
这种设计的主要特点是它将每个功能的逻辑(每个功能由用户在笔记本的一个单独的选项卡上承载)分成不同的类。这些类通过方法调用进行通信,并且连接在构造时是静态的(例如,在构造时将对队列管理器的引用传递给提交控制器)。
这绝不应该是“最好的”设计,它只是可能改进的一个例子 - 这些改进减少了原始类型难以找到逻辑错误的可能性。
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(800, 700))
self.tabbed = wx.Notebook(self, -1, style=(wx.NB_TOP))
self.running = RunningPane(self.tabbed)
self.queue = QueuePane(self.tabbed, self.running)
self.submissions = SubmissionPane(self.tabbed, self.queue)
self.tabbed.AddPage(self.submissions, "Submit Job")
self.tabbed.AddPage(self.queue, "Queue")
self.tabbed.AddPage(self.running, "Running Jobs")
self.CreateStatusBar()
menuBar = wx.MenuBar()
menu = wx.Menu()
self.SetMenuBar(menuBar)
self.Centre()
self.Show()
#---
#
class SubmissionPane(wx.Panel):
def __init__(self, parent, queue_control):
wx.Panel.__init__(self, parent, -1)
self.parent = parent
self.queue_control = queue_control
self.selected_folder = None
self.drop_target = MyFileDropTarget(self)
self.tc_files = wx.TextCtrl(self, wx.ID_ANY, pos=(42, 120), size=(500, 25))
self.tc_files.SetDropTarget(self.drop_target)
self.buttonGo = wx.Button(self, -1, "Submit", pos=(90,530))
self.buttonGo.Bind(wx.EVT_BUTTON, self.OnSubmit)
self.buttonClose = wx.Button(self, -1, "Quit", pos=(195,530))
self.buttonClose.Bind(wx.EVT_BUTTON, self.OnClose)
outputtxt3 = '''Drag & Drop Folder of Packages to Verify'''
wx.StaticText(self, -1, outputtxt3, (33, 64), style=wx.ALIGN_CENTRE)
self.Show()
def SetSubmissionFolders(self, folder_list):
"""Called by the FileDropTarget when files are dropped"""
self.tc_files.SetValue(','.join(folder_list))
self.selected_folders = folder_list
def OnSubmit(self, event):
self.queue_control.QueueFolders(self.selected_folders)
wx.MessageBox('Job Submitted')
def OnClose(self, e):
self.Close(True)
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.SetSubmissionFolders(filenames)
#---
#
class QueuePane(wx.Panel):
def __init__(self, parent, run_control):
wx.Panel.__init__(self, parent, -1)
self.parent = parent
self.run_control = run_control
self.queue = []
self.buttonClose2 = wx.Button(self, -1, "Quit", pos=(195,170))
self.buttonClose2.Bind(wx.EVT_BUTTON, self.OnClose)
self.log_text = wx.TextCtrl(self, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
self.Show()
def QueueFolders(self, folder_list):
"""Called by anyone with a list of folders to queue.
In this code, that is the submission pane."""
self.queue.extend(folder_list)
self.log_text.AppendText("\n".join(folder_list))
def OnClose(self, e):
self.Close(True)
#---
#
class RunningPane(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
self.parent = parent
self.buttonClose3 = wx.Button(self, -1, "Quit", pos=(195,170))
self.buttonClose3.Bind(wx.EVT_BUTTON, self.OnClose)
self.running_log = wx.TextCtrl(self, -1, pos=(35, 210), size=(720,400),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
self.Show()
def OnClose(self, e):
self.Close(True)
#
#---
app = wx.App()
MainWindow(None, -1, 'Application')
app.MainLoop()