python tkinterは簡単な計算機の方法をします。
背景
最近、鶏肉はpython GUIを勉強しています。tkinterから入門して、まずソフトを作って練習したいです。
いろいろ考えて、計算機を作ることにしました。
設計の考え方
まず、私達が必要なカバンを導入します。tkinterを使って、Tkオブジェクトを実装してウィンドウを作成します。
注文がありますので、今は各コンポーネントの位置をコントロールできません。だから、ウィンドウは自動的にデフォルトの大きさを使います。
私の目標はこのような形にすることです。
大体の位置を計画しました。次のように4つのFrameを作りました。
u 1 s 1,3つで十分だと思います。一部は漢字の部分で、ヒント情報はLabelコントロール を使用します。一部は入力ボックスであり、Entryコントロール を使用する。
問題に遭遇しました。ボタンを押しても獲得できないボタンのテキストをクリックしてください。解決後、ブログを書きました。コンベヤー?ドア。
各数字を同じ方法で列挙する
最後の計算は少し怠惰になりました。直接にentry.get()を使って計算する式を得て、eval関数を使って計算します。もしフォーマットが間違っていたら、つまり弾戸提示です。
majer.py
最近、鶏肉はpython GUIを勉強しています。tkinterから入門して、まずソフトを作って練習したいです。
いろいろ考えて、計算機を作ることにしました。
設計の考え方
まず、私達が必要なカバンを導入します。tkinterを使って、Tkオブジェクトを実装してウィンドウを作成します。
注文がありますので、今は各コンポーネントの位置をコントロールできません。だから、ウィンドウは自動的にデフォルトの大きさを使います。
import tkinter as tk
import tkinter.messagebox
win = tkinter.Tk()
win.title(" ")
win.mainloop()
各コンポーネントの位置を大体計画します。私の目標はこのような形にすることです。
大体の位置を計画しました。次のように4つのFrameを作りました。
u 1 s 1,3つで十分だと思います。
#
entry_frame = tk.Frame(win)
#
menu_frame = tk.Frame(win)
#
major_frame = tk.Frame(win)
#
cal_frame = tk.Frame(win)
entry_frame.pack(side="top")
menu_frame.pack(side="left")
major_frame.pack()
cal_frame.pack(side="right")
次は入力ボックスを作って、二つの部分に分けます。
t_label = tk.Label(entry_frame, text = " : ")
t_label.pack(side='left')
word_entry = tk.Entry(
entry_frame,
fg = "blue", # ,
bd = 3, #
width = 39, #
justify = 'right' #
)
word_entry.pack()
演算記号を下の左側に並べます。
for char in ['+', '-', '×', '÷']:
myButton(menu_frame, char, word_entry)
このうち、myButtonクラスはボタンを実用化し、ボタンを押すと、入力ボックスに対応するテキストが表示されます。問題に遭遇しました。ボタンを押しても獲得できないボタンのテキストをクリックしてください。解決後、ブログを書きました。コンベヤー?ドア。
各数字を同じ方法で列挙する
for i in range(4):
num_frame = tk.Frame(major_frame)
num_frame.pack()
if i < 3:
for count in range(3*i+1, 3*i+4):
myButton(num_frame, count, word_entry, side=(i, count))
continue
myButton(num_frame, 0, word_entry, side=(i, 0))
もちろん、リセットボタンと計算ボタンは忘れられません。最後の計算は少し怠惰になりました。直接にentry.get()を使って計算する式を得て、eval関数を使って計算します。もしフォーマットが間違っていたら、つまり弾戸提示です。
def calculate(entry):
try:
result = entry.get()
# , =
if result == '':
return
result = eval(result)
entry.delete(0, "end")
entry.insert(0, str(result))
except:
tkinter.messagebox.showerror(" ", " !
!")
reset_btn = tk.Button(
cal_frame,
text = ' ',
activeforeground = "blue",
activebackground = "pink",
width = "13",
command = lambda :word_entry.delete(0, "end")
).pack(side="left")
result_btn = tk.Button(
cal_frame,
text = '=',
activeforeground = "blue",
activebackground = "pink",
width = "13",
command = lambda :calculate(word_entry)
).pack(side="right")
すべてのコードmajer.py
# -*- coding=utf-8 -*-
# @Time : 2021/3/4 13:06
# @Author : lhys
# @FileName: major.py
myName = r'''
Welcome, my master!
My Name is :
____ ____ ____ ____ ____ ______________
| | | | | | | \ / | / /
| | | | | | | \ / | / /
| | | | | | | \ / | / /
| | | | | | \ \_/ / / _______/
| | | |______| | \ / \ \
| | | | \ / \ \
| | | ______ | \ / \ \
| | | | | | \ / \________ \
| | | | | | | | / /
| |_______ | | | | | | / /
| | | | | | | | / /
|____________| |____| |____| |_____| /______________/
'''
print(myName)
import tkinter as tk
from tools import *
win = tk.Tk()
win.title(' ')
entry_frame = tk.Frame(win)
menu_frame = tk.Frame(win)
major_frame = tk.Frame(win)
cal_frame = tk.Frame(win)
entry_frame.pack(side="top")
menu_frame.pack(side="left")
major_frame.pack()
cal_frame.pack()
#
t_label = tk.Label(entry_frame, text = " : ")
t_label.pack(side='left')
word_entry = tk.Entry(
entry_frame,
fg = "blue",
bd = 3,
width = 39,
justify = 'right'
)
word_entry.pack()
#
for char in ['+', '-', '×', '÷']:
myButton(menu_frame, char, word_entry)
button_side = ['right', 'left']
for i in range(4):
num_frame = tk.Frame(major_frame)
num_frame.pack()
if i < 3:
for count in range(3*i+1, 3*i+4):
myButton(num_frame, count, word_entry, side=(i, count))
continue
myButton(num_frame, 0, word_entry, side=(i, 0))
reset_btn = tk.Button(
cal_frame,
text = ' ',
activeforeground = "blue",
activebackground = "pink",
width = "13",
command = lambda :word_entry.delete(0, "end")
).pack(side="left")
result_btn = tk.Button(
cal_frame,
text = '=',
activeforeground = "blue",
activebackground = "pink",
width = "13",
command = lambda :calculate(word_entry)
).pack(side="right")
win.mainloop()
tools.py
# -*- coding=utf-8 -*-
# @Time : 2021/3/4 13:20
# @Author : lhys
# @FileName: tools.py
import tkinter
import tkinter.messagebox
def calculate(entry):
try:
result = entry.get()
if result == '':
return
result = eval(result)
print(result)
entry.delete(0, "end")
entry.insert(0, str(result))
except:
tkinter.messagebox.showerror(" ", " !
!")
class myButton():
def __init__(self, frame, text, entry, **kwargs):
side = kwargs.get('side') if 'side' in kwargs else ()
self.btn = tkinter.Button(
frame,
text = text,
activeforeground="blue",
activebackground="pink",
width="13",
command=lambda :entry.insert("end", text)
)
if side:
self.btn.grid(row=side[0], column=side[1])
else:
self.btn.pack()
ここで、python tkinterについて簡単な計算機の作り方についての文章を紹介します。python tkinterに関するものがもっと多いです。以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。