0

我正在学习 wxPython,但遇到了问题。下面是我的代码。它真的不起作用。我想要做的是在文本区域下方的左上部分中获得一个按钮,上面写着“输入任务”。我希望文本输入区域和“输入任务”按钮位于一列中,以便两者的组合高度等于它们旁边的开始和退出按钮的高度。有什么帮助吗?尺寸调整器让我感到困惑。

import wx
import datetime

class UpperPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)



        #Timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.Update, self.timer)     

        #Enter Task Field
        self.edit = wx.TextCtrl(self, -1, size=wx.Size(200, -1))
        #Enter Task Button
        self.button = wx.Button(self, -1, label="Enter!")
        self.midpanel = MiddlePanel(self)
        self.button.Bind(wx.EVT_BUTTON, self.midpanel.OnEnterPressed)
        #Start/Stop Button
        self.start_button=wx.Button(self, label="Start", size=(150,50))
        self.Bind(wx.EVT_BUTTON, MiddlePanel.StartButton, self.start_button)
        #Exit Button
        self.exit_button=wx.Button(self, -1, label="Exit", size=(150,50))
        self.Bind(wx.EVT_BUTTON, MainWin.CloseButton, self.exit_button)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.edit, 1, wx.EXPAND)
        self.sizer.Add(self.start_button, 0, wx.EXPAND)
        self.sizer.Add(self.exit_button, 0, wx.EXPAND)

        self.SetSizerAndFit(self.sizer)

#=====================================================================        

class MiddlePanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizerAndFit(self.sizer)

        #Initialize Variables
        self.box_height = 100
        self.box_count = 0
        self.start_time = ''



    def OnEnterPressed(self, event):
        #Get entered text
        uppanel = UpperPanel(self)
        self.box_label = uppanel.edit.GetValue()

        if self.box_count < 10:
            for x in range(0, 1):
                chbx1 = wx.CheckBox(self, -1, self.box_label)
                txtbx1 = wx.TextCtrl(self, -1)
                self.box_height += 30
                uppanel.edit.Clear()
                print("CLEARED!")
                print self.box_count
                print uppanel.edit.GetValue()
                #self.Layout()
            self.box_count += 1

    def StartButton(self,event):
        self.start_time = datetime.datetime.now()
        btn_label = self.start_button.GetLabel()

        if btn_label == "Start":
            print "starting timer..."
            self.timer.Start(1000)
            self.start_button.SetLabel("Stop")
        else:
            print "timer stopped!"
            self.timer.Stop()
            self.start_button.SetLabel("Start")
        print ('Start button pressed.')

    def Update(self, event):       
        seconds = (self.start_time - datetime.datetime.now() +
                   datetime.timedelta(days=1)).seconds
        self.time_text.SetLabel('Time till next task:\n%d hours %d \
minutes %d seconds'
                                % (seconds / 3600,
                                   (seconds / 60)
                                   % (seconds / 3600 * 60),
                                   seconds % 60))





#======================================================================
class MainWin(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        self.middlePanel = MiddlePanel(self)
        self.upperPanel = UpperPanel(self)
        self.textArea = wx.TextCtrl(self, size=(-1, 300), style=wx.TE_MULTILINE)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.upperPanel, 0, wx.EXPAND)
        self.sizer.Add(self.middlePanel, 0, wx.EXPAND)
        self.sizer.Add(self.textArea, 1, wx.EXPAND)

        self.SetSizerAndFit(self.sizer)

        #Window Closer
        self.Bind(wx.EVT_CLOSE, self.CloseWindow)

        #Create Menu Bar
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        firstmenu=wx.Menu()
        secondmenu=wx.Menu()
        firstmenu.Append(wx.NewId(),"Start","Starts the timer.")
        firstmenu.Append(wx.NewId(),"Stop","Stops the timer.")
        firstmenu.Append(wx.NewId(),"Exit","Exits the program.")
        secondmenu.Append(wx.NewId(),"Preferences","Set the timer options.")
        secondmenu.Append(wx.NewId(),"Edit task list",
                          "Add, change, or remove tasks.") 
        menubar.Append(firstmenu,"File")
        menubar.Append(secondmenu,"Edit")
        self.SetMenuBar(menubar)



        #Static Text
        self.st1 = wx.StaticText(self, -1, 'Enter Tasks:')
        self.st2 = wx.StaticText(self, -1, 'Created by Spencer Evans')
        self.time_text = wx.StaticText(self, -1, '')

    def CloseButton(self,event):
        #timer.Stop()
        self.Close(True)
        print ('Closed by Exit button.')

    def CloseWindow(self,event):
        #timer.Stop()
        self.Destroy()
        print ('Closed by X\'ing window.')

#========================================================================
#Run program
if __name__ == '__main__':
    app = wx.PySimpleApp()
    main_win = MainWin()
    main_win.Show()
    app.MainLoop()
4

1 回答 1

0

我真的不知道你的问题在哪里。在我看来,所有已经存在的部分,你只需要把它们放在一起。如果我理解您想要什么,只需将文本字段替换为包含文本字段的面板和一个带有垂直大小调整器的按钮。很好的打字练习,让我提出一个解决方案:

--- /tmp/test.py
+++ /tmp/test2.py
@@ -11,8 +11,19 @@
         self.timer = wx.Timer(self)
         self.Bind(wx.EVT_TIMER, self.Update, self.timer)     

+        # Panel for 'Enter Task' textfield and button
+        enter_task_panel = wx.Panel(self, -1)
+        task_panel_sizer = wx.BoxSizer(wx.VERTICAL)
+        enter_task_panel.SetSizer(task_panel_sizer)
+
         #Enter Task Field
-        self.edit = wx.TextCtrl(self, -1, size=wx.Size(200, -1))
+        self.edit = wx.TextCtrl(enter_task_panel, -1, size=wx.Size(200, -1))
+        task_panel_sizer.Add(self.edit, 1)
+
+        # 'Enter Task' button
+        enter_task_button = wx.Button(enter_task_panel, -1, "Enter Task")
+        task_panel_sizer.Add(enter_task_button, 1, wx.EXPAND)
+
         #Enter Task Button
         self.button = wx.Button(self, -1, label="Enter!")
         self.midpanel = MiddlePanel(self)
@@ -25,7 +36,7 @@
         self.Bind(wx.EVT_BUTTON, MainWin.CloseButton, self.exit_button)

          self.sizer = wx.BoxSizer()
-         self.sizer.Add(self.edit, 1, wx.EXPAND)
+         self.sizer.Add(enter_task_panel, 1, wx.EXPAND)
          self.sizer.Add(self.start_button, 0, wx.EXPAND)
          self.sizer.Add(self.exit_button, 0, wx.EXPAND)

Zetcode有一个很好的 wxPython 教程,我只能推荐。

于 2013-02-17T11:34:07.733 回答