PYTHON GUI Programming

PYTHON GUI Programming 2

Hello everyone, we continue to explore the python GUI programming package used tkinter the third step.

Custom event handlers
to build GUI applications next step is to use the event-defined window. Control can generate events, such as when the user clicks the button, and use the command parameter can specify the name of the method should be called when an event is detected python. For example, we want to be bound to a button event method, the code can be written as:

def creat_widgets(self):
	self.button1 = Button(self,text = 'Submit',
	command = self.display)
	self.button1.grid(row = 1,column = 0,
	sticky = W)
def display(self):
	print('The button clicked in the window')	

Create create_widgets () method of a button in the window display region, the Button class constructor is provided command parameter is self.display, this points class display () method.

At this time, we can write a complete program of the GUI.

from tkinter import *
class Application(Frame):
	'''Built the basic window frame template'''
	def __init__(self,master):
		super(Application,self).__init__(master)
		self.grid()
		self.create_widgets()
		
	def create_widgets(self):
		self.label1 = Label(self,
		text = ('Welcome to my window!')
		self.label1.grid(row = 0,column = 0,sticky = W)
		self.button1 = Button(self,text = 'Submit',
		command = self.display)
		self.button1.grid(row = 1,column = 0,
		sticky = W)
		
	def display(self):
		'''Event handler for the button'''
		print('The button clicked in the window')

root = Tk()
root.title('Test Button events')
root.geometry('300x100')
app = Application(root)
app.mainloop()

Here Insert Picture Description
Here Insert Picture Description
The results code to run
when you click on the Submit button, the display will appear in the window (the contents of the print method).

Here, we have learned to create a simple GUI application using tkinter package, we go and try it

Released three original articles · won praise 2 · Views 261

Guess you like

Origin blog.csdn.net/mantouyouyou/article/details/105212973