Tkinter 计算器 Python 3.4.2
Tkinter Calculator Python 3.4.2
我正在尝试使用 tkinter 在 python 3.4.2 中创建一个程序。我希望创建一个 GUI 计算器。到目前为止,我已经能够编写出程序的按钮和布局(界面)。我唯一需要帮助的是按钮执行的操作。例如,我需要一种方法来将按下的按钮的值输入到计算器顶部的输入栏中。 (例如:我按下按钮 7,然后 7 弹出到我的输入栏中)。我需要帮助编码,因为我不知道如何。到目前为止,我在屏幕上唯一可用的按钮是清除 (ce) 按钮,因为它会清除我 GUI 中的整个输入栏。有人可以看看我下面的代码来帮助我建立一种让这个计算器工作的方法吗? (P.S 我不是 tkinter 的天才,所以基本上试着像对 10 岁的 LOOL 一样向我解释它)。
总结:
- 将我的按钮连接到我的输入栏。
我的代码:
#Project Name : Calculator ++
#Version : 1.7.2
#Written by : Pamal Mangat.
#Start Date : Monday, July 7th, 2015.
import sys
from tkinter import *
from PIL import Image, ImageTk
def clear():
txtDisplay.delete(0,END);
return;
#Parent Window.
root = Tk();
root.title('Calculator ++ [1.7.2]');
root.geometry('350x450');
#Main entry.
num1 = StringVar();
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40);
txtDisplay.place(x=15, y=10);
txtDisplay.focus();
#Buttons:
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red').place(x=17,y=382);
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=302);
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=302);
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=302);
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=222);
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=222);
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=222);
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=142);
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=142);
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=142);
decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue').place(x=182, y=382);
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen').place(x=264, y=307);
plusButton = Button(root, text='+', width=8, height=3, bg='gray').place(x=264, y=222);
minusButton = Button(root, text='-', width=8, height=3, bg='gray').place(x=264, y=142);
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray').place(x=264, y=66);
divideButton = Button(root, text='÷', width=8, height=3, bg='gray').place(x=182, y=66);
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange').place(x=17, y=66);
#Locks the parent windows size.
root.maxsize(350,450);
root.minsize(350,450);
#Parent window's background color:
root.configure(background = 'black');
root.mainloop();
下面是我的代码执行情况和运行后的样子的图像。唯一的问题是它只是一堆没有用的可按按钮;除了我碰巧得到 运行 的 clear(ce)。其他按钮需要意义和功能,因为我真的很想启动这个程序 运行。
正如人们在评论中所写,您需要一个函数来响应按下的按钮并更新 Entry 小部件。我修改了代码,以展示它是如何完成的:
import sys
from tkinter import *
from PIL import Image, ImageTk
def clear():
txtDisplay.delete(0,END);
return;
#Parent Window.
root = Tk();
root.title('Calculator ++ [1.7.2]');
root.geometry('350x450');
#Main entry.
num1 = StringVar();
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40);
txtDisplay.place(x=15, y=10);
txtDisplay.focus();
def update_entry(v):
current_value = num1.get()
num1.set(current_value + v)
#Buttons:
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red', command = lambda: update_entry('0')).place(x=17,y=382);
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red', command = lambda: update_entry('1')).place(x=17, y=302);
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=302);
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=302);
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=222);
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=222);
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=222);
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=142);
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=142);
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=142);
decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue').place(x=182, y=382);
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen').place(x=264, y=307);
plusButton = Button(root, text='+', width=8, height=3, bg='gray', command = lambda: update_entry('+')).place(x=264, y=222);
minusButton = Button(root, text='-', width=8, height=3, bg='gray').place(x=264, y=142);
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray').place(x=264, y=66);
divideButton = Button(root, text='÷', width=8, height=3, bg='gray').place(x=182, y=66);
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange').place(x=17, y=66);
#Locks the parent windows size.
root.maxsize(350,450);
root.minsize(350,450);
#Parent window's background color:
root.configure(background = 'black');
root.mainloop();
我添加了 update_entry
函数来更新 Entry 文本变量。还向按钮 0 和 1 以及 + 添加了 lambda 以作为使用说明。其他按钮没有完成。也不要像现在一样在一行中使用位置(或网格),因为所有 Button 变量都将是 None.
我正在尝试使用 tkinter 在 python 3.4.2 中创建一个程序。我希望创建一个 GUI 计算器。到目前为止,我已经能够编写出程序的按钮和布局(界面)。我唯一需要帮助的是按钮执行的操作。例如,我需要一种方法来将按下的按钮的值输入到计算器顶部的输入栏中。 (例如:我按下按钮 7,然后 7 弹出到我的输入栏中)。我需要帮助编码,因为我不知道如何。到目前为止,我在屏幕上唯一可用的按钮是清除 (ce) 按钮,因为它会清除我 GUI 中的整个输入栏。有人可以看看我下面的代码来帮助我建立一种让这个计算器工作的方法吗? (P.S 我不是 tkinter 的天才,所以基本上试着像对 10 岁的 LOOL 一样向我解释它)。
总结:
- 将我的按钮连接到我的输入栏。
我的代码:
#Project Name : Calculator ++
#Version : 1.7.2
#Written by : Pamal Mangat.
#Start Date : Monday, July 7th, 2015.
import sys
from tkinter import *
from PIL import Image, ImageTk
def clear():
txtDisplay.delete(0,END);
return;
#Parent Window.
root = Tk();
root.title('Calculator ++ [1.7.2]');
root.geometry('350x450');
#Main entry.
num1 = StringVar();
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40);
txtDisplay.place(x=15, y=10);
txtDisplay.focus();
#Buttons:
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red').place(x=17,y=382);
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=302);
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=302);
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=302);
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=222);
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=222);
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=222);
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=142);
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=142);
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=142);
decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue').place(x=182, y=382);
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen').place(x=264, y=307);
plusButton = Button(root, text='+', width=8, height=3, bg='gray').place(x=264, y=222);
minusButton = Button(root, text='-', width=8, height=3, bg='gray').place(x=264, y=142);
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray').place(x=264, y=66);
divideButton = Button(root, text='÷', width=8, height=3, bg='gray').place(x=182, y=66);
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange').place(x=17, y=66);
#Locks the parent windows size.
root.maxsize(350,450);
root.minsize(350,450);
#Parent window's background color:
root.configure(background = 'black');
root.mainloop();
下面是我的代码执行情况和运行后的样子的图像。唯一的问题是它只是一堆没有用的可按按钮;除了我碰巧得到 运行 的 clear(ce)。其他按钮需要意义和功能,因为我真的很想启动这个程序 运行。
正如人们在评论中所写,您需要一个函数来响应按下的按钮并更新 Entry 小部件。我修改了代码,以展示它是如何完成的:
import sys
from tkinter import *
from PIL import Image, ImageTk
def clear():
txtDisplay.delete(0,END);
return;
#Parent Window.
root = Tk();
root.title('Calculator ++ [1.7.2]');
root.geometry('350x450');
#Main entry.
num1 = StringVar();
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40);
txtDisplay.place(x=15, y=10);
txtDisplay.focus();
def update_entry(v):
current_value = num1.get()
num1.set(current_value + v)
#Buttons:
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red', command = lambda: update_entry('0')).place(x=17,y=382);
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red', command = lambda: update_entry('1')).place(x=17, y=302);
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=302);
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=302);
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=222);
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=222);
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=222);
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red').place(x=17, y=142);
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red').place(x=100, y=142);
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red').place(x=182, y=142);
decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue').place(x=182, y=382);
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen').place(x=264, y=307);
plusButton = Button(root, text='+', width=8, height=3, bg='gray', command = lambda: update_entry('+')).place(x=264, y=222);
minusButton = Button(root, text='-', width=8, height=3, bg='gray').place(x=264, y=142);
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray').place(x=264, y=66);
divideButton = Button(root, text='÷', width=8, height=3, bg='gray').place(x=182, y=66);
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange').place(x=17, y=66);
#Locks the parent windows size.
root.maxsize(350,450);
root.minsize(350,450);
#Parent window's background color:
root.configure(background = 'black');
root.mainloop();
我添加了 update_entry
函数来更新 Entry 文本变量。还向按钮 0 和 1 以及 + 添加了 lambda 以作为使用说明。其他按钮没有完成。也不要像现在一样在一行中使用位置(或网格),因为所有 Button 变量都将是 None.