PythonでTkinterを使用したシンプルなGUI計算機
このチュートリアルでは、 Tkinterを使用して簡単なGUI計算機を作成します。 モジュール。 Tkinter GUIアプリケーションを開発するためのPythonモジュールが組み込まれています。使い方は簡単で、Pythonが付属しています。 GUIアプリケーションを使用してデータを視覚化できます。
簡単なGUI計算機を作成する方法を見てみましょう。
-
Tkinterからすべてをインポートします *を使用します。
-
電卓のインターフェースを作成します。
-
入力フィールドに数値を入力する入力関数を作成します。
-
入力フィールドからすべてを消去する見かけの関数を作成します。
-
そして最後に、式の結果を計算して与える関数を評価します。
例
# importing everyting from tkinter
from tkinter import *
# expression to access among all the functions
expression = ""
# functions
def input_number(number, equation):
# accessing the global expression variable
global expression
# concatenation of string
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# setting empty string in the input field
equation.set("Enter the expression")
def evaluate(equation):
global expression
# trying to evaluate the expression
try:
result = str(eval(expression))
# showing the result in the input field
equation.set(result)
# setting expression to empty string
expression = ""
except:
# some error occured
# showing it to the user equation.set("Enter a valid expression")
expression = ""
# creating the GUI
def main():
# main window window = Tk()
# setting the title of GUI window
window.title("Calculator")
# set the configuration of GUI window
window.geometry("325x175")
# varible class instantiation
equation = StringVar()
# input field for the expression
input_field = Entry(window, textvariable=equation)
input_field.place(height=100)
# we are using grid position
# for the arrangement of the widgets
input_field.grid(columnspan=4, ipadx=100, ipady=5)
# settin the placeholder message for users
equation.set("Enter the expression")
# creating buttons and placing them at respective positions
_1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
_1.grid(row=2, column=0)
_2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
_2.grid(row=2, column=1)
_3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
_3.grid(row=2, column=2)
_4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
_4.grid(row=3, column=0)
_5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
_5.grid(row=3, column=1)
_6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
_6.grid(row=3, column=2)
_7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
_7.grid(row=4, column=0)
_8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
_8.grid(row=4, column=1)
_9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
_9.grid(row=4, column=2)
_0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
_0.grid(row=5, column=0)
plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
plus.grid(row=2, column=3)
minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
minus.grid(row=3, column=3)
multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda: input_number('*', equation), height=2, width=7)
multiply.grid(row=4, column=3)
divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
divide.grid(row=5, column=3)
equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
equal.grid(row=5, column=2)
clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
clear.grid(row=5, column=1)
# showing the GUI
window.mainloop()
# start of the program
if __name__ == '__main__':
main()
出力
上記のプログラムを実行すると、次のような簡単な計算機が得られます。
=ボタンを押した後に生成された上記の式の結果。
結論
チュートリアルで疑問がある場合は、コメントセクションでそれらについて言及してください。
-
PythonでTkinterを使用したカラーゲーム
GUIアプリケーションの開発では、tkinterは非常に人気があり簡単です。 tkinterを使用すると、GUIゲームを簡単に開発できます。 ここでもカラーゲームの開発を試みています。このゲームでは、プレーヤーは画面に表示される単語の色を入力する必要があるため、スコアが1つ増えます。このゲームをプレイする合計時間は、30秒で、このゲームで使用される色は、赤、青、緑、ピンク、ブラック、イエロー、オレンジ、ホワイト、パープル、ブラウン。インターフェイスには、さまざまな色の名前がさまざまな色で表示されます。ゲームに勝つには、ユーザーは色を識別し、正しい色の名前を入力する必要があります。 サンプルコ
-
PythonでのCX_Freezeの使用
時々私たちは非常にエキサイティングな何か違うものを作りたいと感じます、そして人間の性質によれば、私たちはいつもそれを共有するのが大好きです。 Pythonもそれらの願いを満たします。 Pythonを使用して、Pythonプログラムを友人と共有したい場合は、それを行うことができます。必要なのは、マシンのプログラムで使用されるすべてのモジュールに同じバージョンのPythonをインストールすることだけです。 まず、 pip install CX_Frezzeを使用してCX_Freezeモジュールをインストールする必要があります コマンドプロンプトのコマンド。 最初のステップは、この割り当て、