我正在学习 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()