【发布时间】:2015-09-25 15:19:31
【问题描述】:
我正在尝试使用 tkinter 在 python 3.4.2 中创建一个程序。我希望创建一个 GUI 计算器。到目前为止,我已经能够编写出程序的按钮和布局(界面)。我唯一需要帮助的是按钮执行的操作。例如,我需要一种方法将按下的按钮的值输入到计算器顶部的输入栏中。 (例如:我按下按钮 7 并且 7 会弹出到我的输入栏中)。我需要帮助编码,因为我不知道如何。到目前为止,我在屏幕上唯一的工作按钮是清除 (ce) 按钮,因为它清除了我的 GUI 中的整个输入栏。有人可以看看我下面的代码,以帮助我建立一种让这个计算器工作的方法。 (PS 我不是 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()。有什么方法你知道吗?
-
那将是
command选项,在documentation for theButtonwidget 中进行了解释。此外,不要将几何管理器方法(即:place())链接到小部件构造的末尾,或者引用都指向None,这是没有用的。 -
哦,所以我应该把我的 .place() 几何方法放在不同的行上,你是什么意思,(指向无)?也感谢您的帮助:)
-
我知道这可能很烦人,但在那篇文章中我没有看到任何关于将按钮连接到任何地方的 entry() 的内容。我知道我可以使用命令方法向按钮发出命令并将其链接到函数或其他任何内容,但是我如何将该按钮直接链接到条目,您能否给我看一个迷你示例或其他什么?这真的会帮助我。
标签: python user-interface python-3.x tkinter calculator