The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Cikgu Amanda Lai, 2021-02-25 06:09:49

How to create GUI - SISTEM BMI

How to create GUI - SISTEM BMI

Python GUI widgets Pn Lai Yen Wei

PYTHON
Download Thonny 2.1.17
http://thonny.org
Sources References :
https://likegeeks.com/python-gui-examples-tkinter-tutorial/#Set-label-font-size
https://www.tutorialspoint.com/python/python_gui_programming.htm
https://dzone.com/articles/python-gui-examples-tkinter-tutorial-like-geeks
https://www.tutorialspoint.com/python/tk_messagebox.htm
PYTHON GUI module

Python provides various options for developing graphical user interfaces (GUIs). Most
important are listed below.

• Tkinter − Tkinter is the Python interface to the Tk GUI toolkit shipped with Python.
We would look this option in this chapter.

• wxPython − This is an open-source Python interface for
wxWindows http://wxpython.org.

• JPython − JPython is a Python port for Java which gives Python scripts seamless
access to Java class libraries on the local machine http://www.jython.org.

Tkinter Widget :

• Canvas – Canvas is used to draw shapes in your GUI.
• Button – Button widget is used to place the buttons in the Tkinter.
• Checkbutton – Checkbutton is used to create the check buttons in your application.

Note that you can select more than one option at a time.
• Entry – Entry widget is used to create input fields in the GUI.
• Frame – Frame is used as containers in the Tkinter.
• Label – Label is used to create a single line widgets like text, images etc.
• Menu – Menu is used to create menus in the GUI.

1

Python GUI widgets Pn Lai Yen Wei

A. How to create a simple Windows GUI

Sistem Pengiraan BMI

Cara menghasilkan antara muka pengguna ( Graphic User interface GUI )
1. import tkinter module to create GUI
tkinter 是 THONNY 本身自己的 MODULE 用来 create GUI.
一定要有这三个 file 最上面

2. Window
用 funtion Tk () 来 create 你 GUI Tk() 是 Thonny 自己的 build in function create GUI
window

Pembolehubahwindow = Tk( )
WindowA = Tk( )
WindowA is the name of the window object .

2

Python GUI widgets Pn Lai Yen Wei

WindowA

3. Label
Create label with text Sistem Pengiraan BMI

Pembolehubah label = lblSistemBMI
Sintaks :
lblSistemBMI = Label ( windowA , text = “ 你的 label 的字 ”)
config - to set the font type, size and color
place – to set the location ( x , y ) of the label
foreground – text color
background – label background color
font types 可以看 microsoftword 里面的 font , 不要选奇怪的或太多 pattern 的 font。
Example set the label font type :
font = “ Rockwell” font size = 18, font foreground color = “Blue”

3

Python GUI widgets Pn Lai Yen Wei

4. Entry
Pembolehubah text box = TxtBerat, TxtTinggi

4

Python GUI widgets Pn Lai Yen Wei
5. Button

这些 command 的是 user define function
我们自己 create function , 当 user 按
button 会去 check_submit( )
Refer : 7. FUNCTION

Button ( command = check_exit)
Command is the action of the button while it click.
Example Button ( command = check_exit)
will call function check_exit create by programmer . refer : 7 Function
# def function all must set before the main program

6. MessageBox
Import a messagebox module to create a messagebox to prompt warning for the user .

Syntax
Here is the simple syntax to create this widget −

tkMessageBox.FunctionName(title, message [, options])

5

Python GUI widgets Pn Lai Yen Wei

Parameters
FunctionName − This is the name of the appropriate message box function.

title − This is the text to be displayed in the title bar of a message box.

message − This is the text to be displayed as a message.

options − options are alternative choices that you may use to tailor a standard message box.
Some of the options that you can use are default and parent. The default option is used to
specify the default button, such as ABORT, RETRY, or IGNORE in the message box. The
parent option is used to specify the window on top of which the message box is to be
displayed.

You could use one of the following functions with dialogue box −

showinfo()
showwarning()
showerror ()
askquestion()
askokcancel()
askyesno ()
askretrycancel ()

7. Function

Define a function that repeat in program create by programmer when button is clicked.

check_exit to close the window
sintaks
windowA.destroy()

check_reset to clear the data entry in the textbox

clear the data entry in the textbox

sintaks :
strTxtBerat.set(“ ” )

6

Python GUI widgets Pn Lai Yen Wei

check_submit function while Button Submit is click by user
记得 def 你的 function , 所有 function 里面的 code 要 indent 进去

7

Python GUI widgets Pn Lai Yen Wei

FULL SOURCE CODE - SISTEM BMI

# import tkinter functions
import tkinter
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox

# Create a windowA with size 400 x 500
windowA = Tk()
windowA.title ("Sistem BMI")
windowA.geometry( "400x500")
windowA.maxsize( 400, 500 )

# def function all must set before the main program
def check_submit() :

berat = str(strTxtBerat.get())
tinggi = str(strTxtTinggi.get())

if berat.isalpha() == True :
messagebox.showerror('Invalid Input', str(berat) +" data salah !")
check_reset()

elif tinggi.isalpha() == True :
messagebox.showerror('Invalid Input', str(tinggi) +" data salah !.")
check_reset()

bmi = float(berat)/float(tinggi)**2
if bmi >= 30 :

result = "Obese"
elif bmi >=25 :

result = "Overweight"
elif bmi >= 18.5 :

result = " Normal Weight"
else :

result = "Underweight"

keputusan = "BMI anda ialah " + format(bmi,".02f") +"\n"+result
lblBMI.config(text= str(keputusan))

def check_exit() :
windowA.destroy()

def check_reset():
strTxtBerat.set("")
strTxtTinggi.set("")
TxtBerat.focus()
lblBMI.config( text="")

8

Python GUI widgets Pn Lai Yen Wei

# To create a label
lblSistemBMI = Label( windowA , text ="Sistem Pengiraan BMI ")
lblSistemBMI.config(font=("Rockwell",18,"bold italic"), foreground="BLUE")
lblSistemBMI.place ( x = 80 , y = 30 )

# To create a label Berat
lblBerat = Label( windowA , text ="Berat (kg) : " )
lblBerat.config (font=("Arial",14), foreground="Black")
lblBerat.place ( x = 30 , y = 70 )

# To create a label Tinggi
lblTinggi = Label( windowA , text ="Tinggi (m) : ")
lblTinggi.config(font=("Arial",14), foreground="Black")
lblTinggi.place ( x = 30 , y = 100 )

# To create a label show result of BMI
lblBMI = Label( windowA , text =" ")
lblBMI.config(font=("Arial",14), foreground="Black")
lblBMI.place ( x = 30 , y = 200 )
# To Create a TextBox for Berat data entry
# TEXTBOX
strTxtBerat = StringVar()
TxtBerat = Entry( windowA, textvariable=strTxtBerat)
TxtBerat.place ( x=150, y=70, width= 150 , height = 25 )

strTxtTinggi = StringVar()
TxtTinggi = Entry( windowA, textvariable=strTxtTinggi)
TxtTinggi.place ( x=150, y=100, width= 150 , height = 25 )

# To Create a Button Submit
# check_submit user define function
btnSubmit =Button( windowA, text="Submit", command = check_submit)
btnSubmit.place( x= 150 , y=160 , width= 80 , height = 30)
btnSubmit.config(state=NORMAL)

# To Create a Button Exit
# check_exit user define function
btnExit =Button( windowA, text="Exit", command = check_exit)
btnExit.place( x= 280 , y=160 , width= 80 , height = 30)
btnExit.config(state=NORMAL)

# To Create a Button Reset
# check_exit user define function
btnReset =Button( windowA, text="Reset", command= check_reset)
btnReset.place( x= 30 , y=160 , width= 80 , height = 30)
btnReset.config(state=NORMAL)

9

Python GUI widgets Pn Lai Yen Wei

B. How to create a new window when a button is click

When button Click Me is clicked , call clickme function to open a new window
Create a new windowB in the function clickme( )

10

Python GUI widgets Pn Lai Yen Wei

11


Click to View FlipBook Version