Hi everyone.
Would really just like someone to take a look at what ive done and tell me how many horrible faux-pas ive made :-)
I'm trying to teach myself the language as im going along which is making it hard to conform to any standards.
I know there are sections that dont work properly as of yet but i am working on those.
I dont fully understand the relevance of 'self' and how to call events (especially across classes!)
I would like my slider to update the StaticText Speed_Label but have so far been unable to write the correct event handler for that.
Anyways... here are the two files. It builds and runs properly (apart from the few bits ive mentioned) but would just like to know if im doing it right... or if its some horrible Frankenstein style code butchery :-p
### SEQUENCE DRIVEN CONTROLLER.PY ###
from Wizard import *
ID_MENU_NEW_WIZ = 101
ID_MENU_SAVE = 102
ID_MENU_LOAD = 103
ID_MENU_EXIT = 104
ID_MENU_HELP = 105
ID_MENU_ABOUT = 106
ID_RADIO_FORWARDS = 201
ID_RADIO_STATIONARY = 202
ID_RADIO_BACKWARDS = 203
ID_SPEED_LABEL = 204
ID_SPEED_SLIDER = 301
ID_SPEED_LABEL = 401
ID_SPEED_JUDDER_BUTTON = 402
# 501-> Reserved for Bristle manual control checkboxes
# 601-> Reserved for Cylinder manual control checkboxes
ID_ACTION_BUTTON_GO = 701
ID_ACTION_BUTTON_STOP = 702
Speed = 3
Cylinders = 2
Bristles = 3
class Frame1(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
Icon = wx.Icon("icon3.ico", wx.BITMAP_TYPE_ICO)
self.SetIcon(Icon)
### MENU ###
# Create a menubar at the top of the frame
MenuBar = wx.MenuBar()
FileMenu = wx.Menu()
HelpMenu = wx.Menu()
# Add items to the menu
FileMenu.Append(ID_MENU_NEW_WIZ, "&New Tractor Wizard\tCtrl-N", "Start the New Tractor wizard")
FileMenu.Append(ID_MENU_SAVE, "&Save\tCtrl-S", "Save the current tractor configuration to file")
FileMenu.Append(ID_MENU_LOAD, "&Load\tCtrl-L", "Open a tractor configuration from file")
FileMenu.AppendSeparator()
FileMenu.Append(ID_MENU_EXIT, "E&xit\tAlt-F4", "Exit the program")
HelpMenu.Append(ID_MENU_HELP, "&Help\tF1", "Open the Help file")
HelpMenu.AppendSeparator()
HelpMenu.Append(ID_MENU_ABOUT, "&About", "About the program")
# Bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnMenuNew_Wiz, id = ID_MENU_NEW_WIZ)
self.Bind(wx.EVT_MENU, self.OnMenuSave, id = ID_MENU_SAVE)
self.Bind(wx.EVT_MENU, self.OnMenuLoad, id = ID_MENU_LOAD)
self.Bind(wx.EVT_MENU, self.OnMenuExit, id = ID_MENU_EXIT)
self.Bind(wx.EVT_MENU, self.OnMenuAbout, id = ID_MENU_ABOUT)
# Put the menu on the menubar
MenuBar.Append(FileMenu, "&File")
MenuBar.Append(HelpMenu, "&Help")
self.SetMenuBar(MenuBar)
### STATUS BAR ###
self.CreateStatusBar()
### MAIN PANEL ###
Main_Panel = wx.Panel(self)
Main_Panel.SetBackgroundColour((225,245,255))
### TITLE PANEL ###
Title_Panel = wx.Panel(Main_Panel)
Title_Label = wx.StaticText(Title_Panel, -1, "Sequence Driven Controller")
Title_Label.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD, True))
Title_Label.SetForegroundColour('#00009F')
Title_Image = wx.Image("DPT Image.jpg", wx.BITMAP_TYPE_ANY)
Title_Bitmap_Widget = wx.StaticBitmap(Title_Panel, -1, wx.BitmapFromImage(Title_Image))
Title_Sizer = wx.BoxSizer(wx.HORIZONTAL)
Title_Sizer.Add(Title_Bitmap_Widget, 1, wx.ALIGN_CENTER)
Title_Sizer.Add(Title_Label, 4, wx.ALIGN_CENTER | wx.LEFT, 10)
Title_Panel.SetSizer(Title_Sizer)
Title_Panel.Layout()
### CONTROL PANEL setup ###
Control_Panel = wx.Panel(Main_Panel)
Control_Sizer = wx.BoxSizer(wx.HORIZONTAL)
### AUTOMATIC CONTROL setup ###
Automatic_Control_Panel = wx.Panel(Control_Panel)
Automatic_Box = wx.StaticBox(Automatic_Control_Panel, -1, 'Automated Control')
Automatic_Box.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, False))
Automatic_Control_Sizer = wx.StaticBoxSizer(Automatic_Box, wx.VERTICAL)
### DIRECTION PANEL ###
Direction_Panel = wx.Panel(Automatic_Control_Panel)
RadioButtonForwards = wx.RadioButton(Direction_Panel, ID_RADIO_FORWARDS, "Forwards")
RadioButtonStationary = wx.RadioButton(Direction_Panel, ID_RADIO_STATIONARY, "Stationary")
RadioButtonBackwards = wx.RadioButton(Direction_Panel, ID_RADIO_BACKWARDS, "Backwards")
Direction_Sizer = wx.BoxSizer(wx.HORIZONTAL)
Direction_Sizer.Add(RadioButtonForwards, 1, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 2)
Direction_Sizer.Add(RadioButtonStationary, 1, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 2)
Direction_Sizer.Add(RadioButtonBackwards, 1, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 2)
Direction_Panel.SetSizer(Direction_Sizer)
Direction_Panel.Layout()
### SPEED SLIDER & JUDDER ###
Speed_Slider_Panel = wx.Panel(Automatic_Control_Panel)
Speed_Slider = wx.Slider(Speed_Slider_Panel, ID_SPEED_SLIDER, 0, 0, 10,
style = wx.HORIZONTAL | wx.SL_AUTOTICKS)
Speed_Slider.SetTickFreq(1, 1)
self.Bind(wx.EVT_SLIDER, self.OnSpeedSlider, id=ID_SPEED_SLIDER)
Speed_Slider_Sizer = wx.BoxSizer(wx.HORIZONTAL)
Speed_Slider_Sizer.Add(Speed_Slider, -1, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 2)
Speed_Slider_Panel.SetSizer(Speed_Slider_Sizer)
Speed_Slider_Panel.Layout()
### SPEED & JUDDER ###
Speed_Judder_Panel = wx.Panel(Automatic_Control_Panel)
Speed_Judder_Sizer = wx.BoxSizer(wx.HORIZONTAL)
Speed_Label = wx.StaticText(Speed_Judder_Panel, ID_SPEED_LABEL, "Speed = %d" % Speed)
Speed_Label.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, False))
Speed_Judder_Button = wx.ToggleButton(Speed_Judder_Panel, ID_SPEED_JUDDER_BUTTON, "Judder")
Speed_Judder_Sizer.Add(Speed_Label, 3, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 2)
Speed_Judder_Sizer.Add(Speed_Judder_Button, 2, wx.EXPAND | wx.ALL, 2)
Speed_Judder_Panel.SetSizer(Speed_Judder_Sizer)
Speed_Judder_Panel.Layout()
### AUTOMATIC CONTROL layout ###
Automatic_Control_Sizer.Add(Direction_Panel, 1, wx.EXPAND)
Automatic_Control_Sizer.Add(Speed_Slider_Panel, 1, wx.EXPAND)
Automatic_Control_Sizer.Add(Speed_Judder_Panel, 1, wx.EXPAND)
Automatic_Control_Panel.SetSizer(Automatic_Control_Sizer)
Automatic_Control_Sizer.Fit(self) # Fit frame to neccessary size
Automatic_Control_Sizer.SetSizeHints(self) # Prevents frame from getting smaller than min.
Automatic_Control_Panel.Layout()
### MANUAL CONTROL ###
Manual_Control_Panel = wx.Panel(Control_Panel)
Bristles_Panel = wx.Panel(Manual_Control_Panel)
Cylinders_Panel = wx.Panel(Manual_Control_Panel)
Manual_Box = wx.StaticBox(Manual_Control_Panel, -1, 'Manual Control')
Manual_Box.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, False))
Manual_Control_Sizer = wx.StaticBoxSizer(Manual_Box, wx.HORIZONTAL)
Bristles_Sizer = wx.BoxSizer(wx.VERTICAL)
Cylinders_Sizer = wx.BoxSizer(wx.VERTICAL)
# Draws manual control check boxes for the exact number of Bristles / Cylinders
# on the machine
Bristles_CheckBox = []
Cylinders_CheckBox = []
for x in range(0, Bristles):
Bristles_CheckBox.insert(x, (wx.CheckBox(Bristles_Panel, 501+x, "Bristle %d" % (x+1))) )
Bristles_Sizer.Add(Bristles_CheckBox[x], 1, wx.ALIGN_CENTER_HORIZONTAL)
for x in range(0, Cylinders):
Cylinders_CheckBox.insert(x, (wx.CheckBox(Cylinders_Panel, 601+x, "Cylinder %d" % (x+1))) )
Cylinders_Sizer.Add(Cylinders_CheckBox[x], 1, wx.ALIGN_CENTER_HORIZONTAL)
Bristles_Panel.SetSizer(Bristles_Sizer)
Bristles_Panel.Layout()
Cylinders_Panel.SetSizer(Cylinders_Sizer)
Cylinders_Panel.Layout()
Manual_Control_Sizer.Add(Cylinders_Panel, 1, wx.EXPAND)
Manual_Control_Sizer.Add(Bristles_Panel, 1, wx.EXPAND)
Manual_Control_Panel.SetSizer(Manual_Control_Sizer)
Manual_Control_Panel.Layout()
## CONTROL PANEL layout ###
Control_Sizer.Add(Automatic_Control_Panel, 3, wx.EXPAND | wx.ALL, 1)
Control_Sizer.Add(Manual_Control_Panel, 2, wx.EXPAND | wx.ALL, 1)
Control_Panel.SetSizer(Control_Sizer)
Control_Panel.Layout()
### ACTION PANEL ###
Action_Panel = wx.Panel(Main_Panel)
Action_Button_Go = wx.Button(Action_Panel, ID_ACTION_BUTTON_GO, "GO")
Action_Button_Go.SetBackgroundColour('Green')
#self.Bind(wx.EVT_BUTTON, self.OnEnterGo, Action_Button_Go)
Action_Button_Stop = wx.Button(Action_Panel, ID_ACTION_BUTTON_STOP, "STOP")
Action_Button_Stop.SetBackgroundColour('Red')
#self.Bind(wx.EVT_BUTTON, self.OnStop, id=ID_ACTION_BUTTON_STOP)
Action_Sizer = wx.BoxSizer(wx.HORIZONTAL)
Action_Sizer.Add(Action_Button_Go, 1, wx.EXPAND | wx.ALL, 1)
Action_Sizer.Add(Action_Button_Stop, 1, wx.EXPAND | wx.ALL, 1)
Action_Panel.SetSizer(Action_Sizer)
Action_Panel.Layout()
### MAIN SIZER CONTROL ###
Main_Sizer = wx.BoxSizer(wx.VERTICAL)
Main_Sizer.Add(Title_Panel, 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
Main_Sizer.Add(Control_Panel, 2, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
Main_Sizer.Add(Action_Panel, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, 10)
Main_Panel.SetSizer(Main_Sizer)
Main_Sizer.Fit(self) # Fit frame to neccessary size
Main_Sizer.SetSizeHints(self) # Prevents frame from getting smaller than min.
### MENU EVENTS ###
def OnMenuNew_Wiz(self, event):
Wizard_Run()
def OnMenuSave(self, event):
dlg = wx.FileDialog(self, "Save Config File...", os.getcwd(),
style = wx.SAVE | wx.CHANGE_DIR | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
print "Saved"
wx.Sleep(1)
dlg.Destroy()
def OnMenuLoad(self, event):
dlg = wx.FileDialog(self, "Load Config File...", os.getcwd(),
style = wx.OPEN | wx.CHANGE_DIR)
if dlg.ShowModal() == wx.ID_OK:
print "Loaded"
wx.Sleep(1)
dlg.Destroy()
def OnMenuExit(self, event):
self.Close()
def OnMenuAbout(self, evt):
dlg = wx.MessageDialog(self, "DPT - Sequence Driven Controller\n"
"Mark Walker 2006/07\n"
"Final Year Engineering Project", "Sequence Driven Controller",
wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
### CONTROLLER DRIVEN EVENTS ###
def OnSpeedSlider(self, event):
Speed_Label.SetLabel("Speed = %d" % event.Speed_Slider.GetValue())
class wxPyApp(wx.App):
def OnInit(self):
frame = Frame1(None, "DPT - Sequence Driven Controller")
self.SetTopWindow(frame)
frame.Show(True)
frame.Center(wx.BOTH)
return True
if __name__ == '__main__': # Only runs if program is being executed, to imported
app = wxPyApp(redirect=True)
app.MainLoop()
### WIZARD.PY ###
import wx
import wx.wizard
import os
ID_SPIN_BRI = 801
ID_SPIN_CYL = 802
### WIZARD BUILD ###
def Wizard_Run():
app = wx.PySimpleApp()
Wizard = wx.wizard.Wizard(None, -1, "DPT New Tractor Wizard")
Wiz_Page_1 = Wizard_Page(Wizard, "Welcome")
Wiz_Page_1.sizer.Add(wx.StaticText(Wiz_Page_1, -1,
"Welcome to the DPT New Tractor Wizard.\n\n"
"This Wizard will walk you through the \n"
"process of adding a new tractor to the\n"
"controller's database.\n"
"You will be asked to input the sequence\n"
"information for the Startup, Forwards,\n"
"Backwards, Forwards to Backwards and \n"
"Backwards to Forwards routines.\n\n\n"
"Click 'Next' to continue...",
(-1, -1), (-1,-1), wx.ALIGN_CENTER), wx.LEFT, 25)
Wiz_Page_2 = Wizard_Page(Wizard, "Bristles and Cylinders")
Wiz_Page_2.sizer.Add(wx.StaticText(Wiz_Page_2, -1, "\n\nEnter the correct number of Bristle Sections:\n "))
Wiz_Page_2.sizer.Add(wx.SpinCtrl(Wiz_Page_2, ID_SPIN_BRI, "", min = 0, max = 10))
#Wizard_Page.Bind(wx.EVT_SPINCTRL, Wizard_Page.OnSpinBri, id=ID_SPIN_BRI)
Wiz_Page_2.sizer.Add(wx.StaticText(Wiz_Page_2, -1, "\n\nEnter the correct number of Cylinders:\n "))
Wiz_Page_2.sizer.Add(wx.SpinCtrl(Wiz_Page_2, ID_SPIN_CYL, "", min = 0, max = 10))
Bristles = 4
#Wizard_Page.Bind(wx.EVT_SPINCTRL, Wizard_Page.OnSpinCyl, id=ID_SPIN_CYL)
wx.wizard.WizardPageSimple_Chain(Wiz_Page_1, Wiz_Page_2)
Wizard.FitToPage(Wiz_Page_1)
if Wizard.RunWizard(Wiz_Page_1):
print "Success"
#self.Frame1.Refresh()
### WIZARD CLASS DEFINITION ###
class Wizard_Page(wx.wizard.WizardPageSimple):
def __init__(self, parent, title):
wx.wizard.WizardPageSimple.__init__(self, parent)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.sizer)
TitleText = wx.StaticText(self, -1, title)
TitleText.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.BOLD))
self.sizer.Add(TitleText, 0, wx.ALIGN_CENTER | wx.ALL, 5)
self.sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND | wx.ALL, 5)
Thanks for any help,
Mark