溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用python?tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

發(fā)布時(shí)間:2022-02-07 15:40:37 來源:億速云 閱讀:175 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“怎么用python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能”吧!

效果圖

怎么用python?tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

直接上代碼

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


def append_num(num):
    global input_num_ls
    if len(num) < 10:
        input_num_ls.append(num)
    else:
        input_num_ls.append(num[:10])

    current_value.set(get_num(input_num_ls))

    print(input_num_ls)


def append_calculator(method):
    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)
        input_num_ls.append(str(second_num + first_num))

    elif calculator_method == '-':
        current_value.set(first_num - second_num)
        input_num_ls.append(str(first_num - second_num))

    elif calculator_method == '*':
        current_value.set(first_num * second_num)
        input_num_ls.append(str(second_num * first_num))

    elif calculator_method == '/':
        current_value.set(first_num / second_num)
        input_num_ls.append(str(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()

# 設(shè)置窗口 標(biāo)題
window.title('簡(jiǎn)易計(jì)算器')

# 設(shè)置窗口 寬高
window.geometry('400x300')

# 添加user顯示屏幕背景
screen_area = tk.Frame(width='400', height='100', bg='#ddd')
# 放置到window中
screen_area.pack()

# 示例設(shè)置顯示的數(shù)據(jù)類
current_value = tk.StringVar()
current_value.set(0)

# 數(shù)字顯示框
# anchor  文本相對(duì)于標(biāo)簽中心的位置   默認(rèn)是center N S W E
show_screen_label = tk.Label(screen_area, textvariable=current_value, bg='white', width='400', height='2', font={'黑體', 40, 'bold'}, anchor='e')
show_screen_label.pack(padx=10, pady=6)

# 按鍵區(qū)域
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: append_calculator('+')).grid(row='1', column='1')
tk.Button(button_area, text='-', width='5', height='1', command=lambda: append_calculator('-')).grid(row='1', column='2')
tk.Button(button_area, text='*', width='5', height='1', command=lambda: append_calculator('*')).grid(row='1', column='3')
tk.Button(button_area, text='7', width='5', height='1', command=lambda: append_num('7')).grid(row='2', column='0')
tk.Button(button_area, text='8', width='5', height='1', command=lambda: append_num('8')).grid(row='2', column='1')
tk.Button(button_area, text='9', width='5', height='1', command=lambda: append_num('9')).grid(row='2', column='2')
tk.Button(button_area, text='/', width='5', height='1', command=lambda: append_calculator('/')).grid(row='2', column='3')
tk.Button(button_area, text='4', width='5', height='1', command=lambda: append_num('4')).grid(row='3', column='0')
tk.Button(button_area, text='5', width='5', height='1', command=lambda: append_num('5')).grid(row='3', column='1')
tk.Button(button_area, text='6', width='5', height='1', command=lambda: append_num('6')).grid(row='3', column='2')
tk.Button(button_area, text='=', width='5', height='1', command=lambda: calculator_result()).grid(row='3', column='3')
tk.Button(button_area, text='1', width='5', height='1', command=lambda: append_num('1')).grid(row='4', column='0')
tk.Button(button_area, text='2', width='5', height='1', command=lambda: append_num('2')).grid(row='4', column='1')
tk.Button(button_area, text='3', width='5', height='1', command=lambda: append_num('3')).grid(row='4', column='2')
tk.Button(button_area, text='C', width='5', height='1', command=lambda: clear()).grid(row='4', column='3')

window.mainloop()

感謝各位的閱讀,以上就是“怎么用python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用python tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI