【问题标题】:Tkinter Calculator Python 3.4.2Tkinter 计算器 Python 3.4.2
【发布时间】:2015-09-25 15:19:31
【问题描述】:

我正在尝试使用 tkinter 在 python 3.4.2 中创建一个程序。我希望创建一个 GUI 计算器。到目前为止,我已经能够编写出程序的按钮和布局(界面)。我唯一需要帮助的是按钮执行的操作。例如,我需要一种方法将按下的按钮的值输入到计算器顶部的输入栏中。 (例如:我按下按钮 7 并且 7 会弹出到我的输入栏中)。我需要帮助编码,因为我不知道如何。到目前为止,我在屏幕上唯一的工作按钮是清除 (ce) 按钮,因为它清除了我的 GUI 中的整个输入栏。有人可以看看我下面的代码,以帮助我建立一种让这个计算器工作的方法。 (PS 我不是 tkinter 的天才,所以基本上试着向我解释,就像你对 10 岁的 LOOL 一样)。

总结:

  1. 将我的按钮连接到我的输入栏。

我的代码:

#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 the Button widget 中进行了解释。此外,不要将几何管理器方法(即:place())链接到小部件构造的末尾,或者引用都指向None,这是没有用的。
  • 哦,所以我应该把我的 .place() 几何方法放在不同的行上,你是什么意思,(指向无)?也感谢您的帮助:)
  • 我知道这可能很烦人,但在那篇文章中我没有看到任何关于将按钮连接到任何地方的 entry() 的内容。我知道我可以使用命令方法向按钮发出命令并将其链接到函数或其他任何内容,但是我如何将该按钮直接链接到条目,您能否给我看一个迷你示例或其他什么?这真的会帮助我。

标签: python user-interface python-3.x tkinter calculator


【解决方案1】:

正如 ppl 在 cmets 中所写,您需要一个函数来响应按下的按钮并更新 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 函数。还在按钮 0 和 1 和 + 中添加了 lambda,以供使用。其他按钮没有做。也不要像现在那样在一行中使用位置(或网格),因为所有 Button 变量都将为无。

【讨论】:

  • 好的,我已经更新了我的代码,并且我已经将我的几何方法 (.place(x=#, y=#)) 放在了不同的行上,就像请求的那样,我已经更新了函数以运行所有按钮。现在唯一需要完成的是程序的实际数学部分。就像我将如何在输入栏中显示答案一样。我知道如何使用 4+4=8 来做到这一点,但是如果用户输入多个数字,例如 e,g : 2+2*6 怎么办。我将如何只显示答案?
猜你喜欢
  • 2016-12-09
  • 1970-01-01
  • 2021-03-25
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 2013-11-24
  • 2015-09-29
相关资源
最近更新 更多