a_4_button_gui
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
a_4_button_gui [2016/08/10 20:45] – walkeradmin | a_4_button_gui [2023/03/09 22:35] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
\\ | \\ | ||
\\ | \\ | ||
- | ** NOTE ** You need Python 3 for this project, the syntax wont work in Python 2 (but can be changed If you know the differences) | + | ** NOTE ** You need **Python 3** for this project, the syntax wont work in Python 2 (but can be changed If you know the differences) |
\\ | \\ | ||
\\ | \\ | ||
Line 9: | Line 9: | ||
\\ | \\ | ||
This is an example of using something in Python called Tkinter, which has many funcitons for creating GUI's (forms, labels, buttons etc) and the following code will create a 4 button app where a function is executed depending on what button is pressed. | This is an example of using something in Python called Tkinter, which has many funcitons for creating GUI's (forms, labels, buttons etc) and the following code will create a 4 button app where a function is executed depending on what button is pressed. | ||
+ | \\ | ||
+ | \\ | ||
+ | This GUI can be used on an LCD Touchscreen, | ||
\\ | \\ | ||
\\ | \\ | ||
Line 24: | Line 27: | ||
* Sets button 2 in the bottom frame | * Sets button 2 in the bottom frame | ||
\\ | \\ | ||
- | \\ | ||
===== See the Program in Action ===== | ===== See the Program in Action ===== | ||
\\ | \\ | ||
+ | {{: | ||
+ | {{: | ||
\\ | \\ | ||
===== The Code ===== | ===== The Code ===== | ||
\\ | \\ | ||
- | You can download the code and icon files {{ :40x2_lcd.zip |Here:}} | + | You can download the code and icon files {{ :4buttongui.zip |Here}} |
---- | ---- | ||
- | # Python 3.4.2\\ | + | <sxh [py][; options for SyntaxHighlighter]> |
- | #\\ | + | # Python 3.4.2 |
- | # Test programme for doing a simple 4 button GUI\\ | + | # |
- | #\\ | + | # Test programme for doing a simple 4 button GUI |
- | # Alan Walker\\ | + | # |
- | # Aug 2016\\ | + | # Alan Walker |
- | #\\ | + | # Aug 2016 |
- | #\\ | + | # |
- | # This programe creates a root window, then creates two frame in that root window (topFrame and bottomFrame)\\ | + | # |
- | # Then four buttons are created, two in the top frame, two in the bottom. An image is loaded on to each button\\ | + | # This programe creates a root window, then creates two frame in that root window (topFrame and bottomFrame) |
- | # Each button is assigned to a function so that something happens when the buttons are clicked.\\ | + | # Then four buttons are created, two in the top frame, two in the bottom. An image is loaded on to each button |
- | # The buttons are positioned left and right so you have a 2x2 layout\\ | + | # Each button is assigned to a function so that something happens when the buttons are clicked. |
- | #\\ | + | # The buttons are positioned left and right so you have a 2x2 layout |
- | # Thats it, this is just a tester for my LCD screen that I want to use for my Intervalometer.\\ | + | # |
- | #\\ | + | # Thats it, this is just a tester for my LCD screen that I want to use for my Intervalometer. |
- | #\\ | + | # |
- | \\ | + | # |
- | from tkinter import * #import libraries\\ | + | |
- | \\ | + | |
- | def btn1click(): | + | |
- | print(" | + | |
- | \\ | + | |
- | def btn2click(): | + | |
- | print(" | + | |
- | \\ | + | |
- | def btn3click(): | + | |
- | print(" | + | |
- | \\ | + | |
- | def btn4click(): | + | |
- | print(" | + | |
- | \\ | + | |
- | \\ | + | |
- | root = Tk() #This is the root window, all the frames will be in this window\\ | + | |
- | \\ | + | |
- | topFrame = Frame(root) #This project will have 2 frames, an upper and a lower. 2 icons in top, 2 in bottom\\ | + | |
- | #without the frames, its impossible to organise the 4 icons in a 2x2 configuration\\ | + | |
- | #topFrame is the frame at the top, Frame=(root) means the frame is in the root window\\ | + | |
- | topFrame.pack() | + | from tkinter import * #import libraries |
- | bottomFrame = Frame (root) | + | |
- | bottomFrame.pack(side=BOTTOM) #place the bottom frame at the bottom (by default, the only other frame will be at the top)\\ | + | def btn1click(): |
- | \\ | + | print(" |
- | button1 = Button(topFrame, | + | |
- | #call it button 1, make the forground colour (text) red.\\ | + | def btn2click(): |
- | button1.img = PhotoImage(file=" | + | print(" |
- | button1.config(image=button1.img) | + | |
- | button1.config(command=btn1click) | + | def btn3click(): |
- | \\ | + | print(" |
- | button2 = Button(topFrame, | + | |
- | button2.img = PhotoImage(file=" | + | def btn4click(): |
- | button2.config(image=button2.img)\\ | + | print(" |
- | button2.config(command=btn2click)\\ | + | |
- | \\ | + | |
- | button3 = Button(bottomFrame, | + | root = Tk() #This is the root window, all the frames will be in this window |
- | button3.img = PhotoImage(file=" | + | |
- | button3.config(image=button3.img)\\ | + | topFrame = Frame(root) #This project will have 2 frames, an upper and a lower. 2 icons in top, 2 in bottom |
- | button3.config(command=btn3click)\\ | + | #without the frames, its impossible to organise the 4 icons in a 2x2 configuration |
- | \\ | + | #topFrame is the frame at the top, Frame=(root) means the frame is in the root window |
- | button4 = Button(bottomFrame, | + | |
- | button4.img = PhotoImage(file=" | + | topFrame.pack() |
- | button4.config(image=button4.img)\\ | + | bottomFrame = Frame (root) |
- | button4.config(command=btn4click) | + | bottomFrame.pack(side=BOTTOM) #place the bottom frame at the bottom (by default, the only other frame will be at the top) |
- | \\ | + | |
- | button1.pack(side=LEFT) | + | button1 = Button(topFrame, |
- | button2.pack(side=RIGHT) | + | #call it button 1, make the forground colour (text) red. |
- | button3.pack(side=LEFT)\\ | + | button1.img = PhotoImage(file=" |
- | button4.pack(side=RIGHT)\\ | + | button1.config(image=button1.img) |
+ | button1.config(command=btn1click) | ||
+ | |||
+ | button2 = Button(topFrame, | ||
+ | button2.img = PhotoImage(file=" | ||
+ | button2.config(image=button2.img) | ||
+ | button2.config(command=btn2click) | ||
+ | |||
+ | button3 = Button(bottomFrame, | ||
+ | button3.img = PhotoImage(file=" | ||
+ | button3.config(image=button3.img) | ||
+ | button3.config(command=btn3click) | ||
+ | |||
+ | button4 = Button(bottomFrame, | ||
+ | button4.img = PhotoImage(file=" | ||
+ | button4.config(image=button4.img) | ||
+ | button4.config(command=btn4click) | ||
+ | |||
+ | button1.pack(side=LEFT) | ||
+ | button2.pack(side=RIGHT) | ||
+ | button3.pack(side=LEFT) | ||
+ | button4.pack(side=RIGHT) | ||
+ | |||
+ | root.mainloop() | ||
+ | |||
+ | # | ||
+ | # | ||
+ | </ | ||
\\ | \\ | ||
- | root.mainloop()\\ | ||
\\ | \\ | ||
a_4_button_gui.1470858325.txt.gz · Last modified: 2023/03/09 22:35 (external edit)