程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Tkinter module exercise (Python): simple calculator

編輯:Python

Simple calculator production

  • Pre knowledge
    • Entry Introduce
    • Label Introduce
      • Use the syntax
      • Parameter description
    • Button Introduce
      • Use the syntax
      • Parameter description
  • Design thinking
  • Code
  • Effect display
  • link

Pre knowledge

Entry Introduce

  • Entry Used to enter a single line of text
Entry(root, text='input your text here').pack()
  • stay Entry Set initial value in , Use textvariable Combine variables with Entry binding , Determine the input variable type
e = StringVar()
entry = Entry(root, textvariable=e).pack()
e.set('input your text here')
  • Set the status to read-only
e2 = StringVar()
entry = Entry(root, textvariable=e2)
e2.set('not modify')
entry.pack()
entry['state'] = 'readonly'
  • For others, please see the following information link

Label Introduce

Use the syntax

widget = Radiobutton( master, parameter=value, ... )
  • master: The parent container of the text box control
  • parameter: Parameters of the text box
  • value: The corresponding value of the parameter

Parameter description

  • text Label text , You can add text to the label
  • relief Label style , Setting controls 3D effect , Optional :FLAT、SUNKEN、RAISED、GROOVE、RIDGE.
  • font Label text font settings ,font=(‘ typeface ’, Font size , ‘bold/italic/underline/overstrike’)
  • For others, please see the following information link

Button Introduce

Use the syntax

widget = Button( master, parameter=value, ... )
  • master: The parent container of the button control
  • parameter: Button parameters
  • value: The corresponding value of the parameter

Parameter description

  • state Button status options , The state is DISABLED/NORMAL/ACTIVE
  • command The function associated with the button , When the button is clicked , Execute this function
  • For others, please see the following information link

Design thinking

  1. First , Import the package we need — tkinter, And pass Instantiate a Tk object create a window
import tkinter
# create a window
root = tkinter.Tk()
# Create a framework
frame = tkinter.Frame(root)
root.mainloop()
  1. roughly planning Of each component Location
  2. Code design , Input and so on

Code

import tkinter
# create a window
root = tkinter.Tk()
# Create a framework
frame = tkinter.Frame(root)
# Place the frame
frame.pack(padx= 20,pady = 10)
# Create variables
v1 = tkinter.StringVar()
v2 = tkinter.StringVar()
v3 = tkinter.StringVar()
# Create a label
L1 = tkinter.Label(frame,text = ' Simple calculator ',font = ' Microsoft YaHei ,30')
L2 = tkinter.Label(frame,text = '=')
# Place labels
L1.grid(row = 0,column = 2,pady = 10,padx = 10)
L2.grid(row = 1,column = 2,padx= 10,pady = 10)
# Define the filtering method
def text(content):
return content.isdigit()
# Create an input window
e1 = tkinter.Entry(frame,width = 10,textvariable = v1,validate = 'key',validatecommand = (text,'%P'))
e2 = tkinter.Entry(frame,width = 10,textvariable = v2,validate = 'key',validatecommand = (text,'%P'))
e3 = tkinter.Entry(frame,width = 10,textvariable = v3,validate = 'key',validatecommand = (text,'%P'),state ='readonly')
# Placement input
e1.grid(row = 1,column = 0,pady =10,padx = 10)
e2.grid(row = 1,column = 1,padx = 10,pady = 10)
e3.grid(row = 1,column = 3,padx = 10,pady = 10)
# Define the calculation method
def add():
result = int(v1.get()) + int(v2.get())
v3.set(result)
def subtract():
result = int(v1.get()) - int(v2.get())
v3.set(result)
def ride():
result = int(v1.get()) * int(v2.get())
v3.set(result)
def division():
result = int(v1.get()) / int(v2.get())
v3.set(result)
# Create keys
b0 = tkinter.Button(frame,text = '+',command = add,bd = 5)
b1 = tkinter.Button(frame,text = '-',command = subtract,bd = 5)
b2 = tkinter.Button(frame,text = '*',command = ride,bd = 5)
b3 = tkinter.Button(frame,text = '/',command = division,bd = 5)
# Place the keys
b0.grid(row = 2,column = 0,padx = 10,pady = 10)
b1.grid(row = 2,column = 1,pady = 10)
b2.grid(row = 2,column = 2,pady = 10)
b3.grid(row = 2,column = 3,pady = 10)
# Enter the message loop
root.mainloop()

Effect display

link

tkinter Control

Python Tkinter Grid Layout manager details


If this article is helpful to my friends , I hope you can give me some praise and support ~ Thank you very much. ~



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved