I've been trying to step into learning wxpython, so I followed some examples on the web and put together this very simple application. The good news for me is that it is working almost exactly as I would expect it to. I do have a couple of questions for the community.

1. When I run this fine piece of work, everything is pushed up against the top margin on my window. What do I need to do to get things to move down just a little bit?

2. The only way I could get my process function to display the values in the textbox was to put the self. in front of the two text boxes. Is that the right way to do it? Should I have tried to pass values to the function instead?

3. Any suggestions on what I could do to make my code better? I don't want to write crap code. Did I do anything wrong?

Your comments are appreciated

import wx

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(400,200), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
		
		#-- I think you need a panel if you're going to have more than one control in your frame.
		panel = wx.Panel(self, -1)
		
		#--- Create the Username label and text box and add them to the panel
		#--- I think the TextCtrl needs to have self. at the front for the value to be used by
		#--- the Process() function below.  There must be a better way.
		
		self.txt_Username = wx.TextCtrl(panel, 1, size=(125, -1))
		lbl_Username = wx.StaticText(panel, -1, "Username:")
		
		#-- Create the Password label and text box and add them to the panel
		self.txt_Password = wx.TextCtrl(panel, 1, size=(125, -1), style=wx.TE_PASSWORD)
		lbl_Password = wx.StaticText(panel, -1, "Password:")
		
		#-- Create the processing button, add it to the panel and wire it up to a function in the class
		btn_Process = wx.Button(panel, -1, "&Process")
		self.Bind(wx.EVT_BUTTON, self.Process, btn_Process)
		
		#-- Create the close button, add it to the panel and wire it up to a function in the class
		btn_Close = wx.Button(panel, -1, "&Close")
		self.Bind(wx.EVT_BUTTON, self.onExit, btn_Close)
		
		#-- Now we have to create a grid to layout our controls
		sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=10)
		sizer.Add(lbl_Username)
		sizer.Add(self.txt_Username)
		sizer.Add(lbl_Password)
		sizer.Add(self.txt_Password)
		sizer.Add(btn_Process)
		sizer.Add(btn_Close)
		
		#-- Add the grid to the panel and make it fit
		panel.SetSizer(sizer)
		
		#-- Show the window that we've just built
		self.Show(True)
		
	def Process(self, event):
		UserText = self.txt_Username.GetValue()
		PasswordText = self.txt_Password.GetValue()
		d = wx.MessageDialog(self, PasswordText, UserText, wx.OK)
		d.ShowModal()
		d.Destroy()
	
	def onExit(self, event):
		self.Close(True)

app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Enter Credentials")
app.MainLoop()

Proportionality is missing. Also you can add wx.EXPAND for widgets to fit your sizer better

import wx

class MainWindow(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(400,200), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
		
		#-- I think you need a panel if you're going to have more than one control in your frame.
		panel = wx.Panel(self, -1)
		
		#--- Create the Username label and text box and add them to the panel
		#--- I think the TextCtrl needs to have self. at the front for the value to be used by
		#--- the Process() function below.  There must be a better way.
		
		self.txt_Username = wx.TextCtrl(panel, 1, size=(125, -1))
		lbl_Username = wx.StaticText(panel, -1, "Username:")
		
		#-- Create the Password label and text box and add them to the panel
		self.txt_Password = wx.TextCtrl(panel, 1, size=(125, -1), style=wx.TE_PASSWORD)
		lbl_Password = wx.StaticText(panel, -1, "Password:")
		
		#-- Create the processing button, add it to the panel and wire it up to a function in the class
		btn_Process = wx.Button(panel, -1, "&Process")
		self.Bind(wx.EVT_BUTTON, self.Process, btn_Process)
		
		#-- Create the close button, add it to the panel and wire it up to a function in the class
		btn_Close = wx.Button(panel, -1, "&Close")
		self.Bind(wx.EVT_BUTTON, self.onExit, btn_Close)
		
		#-- Now we have to create a grid to layout our controls
		#format--> sizer.Add(widget_to_add, proportion,spacing btn widgets )
		#Also using Flexigrid sizer use AddMany for simplicity
		sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=10)
		sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 5)
		sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 5)
		sizer.Add(lbl_Password,0, wx.LEFT|wx.TOP| wx.RIGHT, 5)
		sizer.Add(self.txt_Password,0, wx.TOP| wx.RIGHT, 5)
		sizer.Add(btn_Process,0, wx.LEFT|wx.TOP| wx.RIGHT, 5)
		sizer.Add(btn_Close,0, wx.TOP| wx.RIGHT, 5)
		
		#-- Add the grid to the panel and make it fit
		panel.SetSizer(sizer)
		
		#-- Show the window that we've just built
		self.Show(True)
		
	def Process(self, event):
		UserText = self.txt_Username.GetValue()
		PasswordText = self.txt_Password.GetValue()
		d = wx.MessageDialog(self, PasswordText, UserText, wx.OK)
		d.ShowModal()
		d.Destroy()
	
	def onExit(self, event):
		self.Close(True)

app = wx.PySimpleApp()
frame = MainWindow(None,-1,"Enter Credentials")
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.