教你python tkinter實現簡單計算器功能

2022-09-20 15:24:15 字數 2816 閱讀 9018

這篇文章主要為大家詳細介紹了python tkinter實現簡單計算器功能,文中示例**介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下

效果圖

直接上**

import tkinter as tk

input_num_ls =

first_num = none

calculator_method = none

def get_num(ls):

new_ls = [10 ** i * float(num) for i, num in enumerate(ls)]

ls_sum = sum(new_ls)

if int(ls_sum) == ls_sum:

return int(ls_sum)

else:

return ls_sum

global input_num_ls

if len(num) < 10:

else:

current_value.set(get_num(input_num_ls))

print(input_num_ls)

global input_num_ls, first_num, calculator_method

calculator_method = method

first_num = get_num(input_num_ls)

input_num_ls =

print('method', calculator_method)

def calculator_result():

global first_num, input_num_ls, calculator_method

second_num = get_num(input_num_ls)

input_num_ls.clear()

if calculator_method == '+':

current_value.set(second_num + first_num)

elif calculator_method == '-':

current_value.set(first_num - second_num)

elif calculator_method == '*':

current_value.set(first_num * second_num)

elif calculator_method == '/':

current_value.set(first_num / second_num)

print(first_num, second_num, calculator_method)

def clear():

global first_num, input_num_ls, calculator_method

first_num = none

input_num_ls =

calculator_method = none

current_value.set(0)

def func():

pass

# 主體視窗

window = tk.tk()

# 設定視窗 標題

window.title('簡易計算器')

# 設定視窗 寬高

window.geometry('400x300')

# 新增user顯示螢幕背景

screen_area = tk.frame(width='400', height='100', bg='#ddd')

# 放置到window中

screen_area.pack()

# 示例設定顯示的資料類

current_value = tk.strin**ar()

current_value.set(0)

# 數字顯示框

# anchor 文字相對於標籤中心的位置 預設是center n s w e

show_screen_label = tk.label(screen_area, textvariable=current_value, bg='white', width='400', height='2', font=, anchor='e')

show_screen_label.pack(padx=10, pady=6)

# 按鍵區域

button_area = tk.frame(width='300', height='300', bg='#ccc')

button_area.pack(padx=10, pady=5)

# 新增button

tk.button(button_area, text='c', width='5', height='1', command=lambda: clear()).grid(row='1', column='0')

tk.button(button_area, text='=', width='5', height='1', command=lambda: calculator_result()).grid(row='3', column='3')

tk.button(button_area, text='c', width='5', height='1', command=lambda: clear()).grid(row='4', column='3')

window.mainloop()

Python Tkinter 布局方式

優點問題,如何在介面上顯示 定義 所有的tkinter 元件都包含專用的幾何管理方法,這些方法是用來組織和管理整個父配件區中子配件的布局的。tkinter 提供了截然不同的三種幾何管理類 pack grid 和place。pack 幾何管理採用塊的方式組織配件,在快速生成介面設計中廣泛採用,若干元件...

python tkinter 單選 多選

單選按鈕 tkinter.radiobutton root,text a pack tkinter.radiobutton root,text b pack tkinter.radiobutton root,text c pack 預設被選中,且單個一組 variable把radiobutton分成...

Python tkinter例項 簡單介紹

usr bin env python3.3 coding utf 8 import tkinter as tk from tkinter import ttk def sample1 hello title 1,最簡單 root tk.tk root.title 你好,這是tkinter世界 roo...