如何根据调用它的 Button 执行更多命令?(类和 tkinter) [英] How can I execute more a command based on the Button that called it? (Classes and tkinter)

查看:44
本文介绍了如何根据调用它的 Button 执行更多命令?(类和 tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学 Python,目前正在开展我的第一个项目.我正在用 Python 制作一个计算器应用程序,我称之为 Pythulator.我试图弄清楚如何在按下按钮时将数字键盘的相应数字连接到数字字符串.我创建了一个名为 numpad 的类来存储我的数字键盘,并将其显示在 GUI 上,但是每当我在数字键盘上按下一个数字时,控制台中都不会打印任何内容.代码如下:

I am teaching myself Python and am currently working on my first project. I am making a calculator application in Python which I am calling Pythulator. I am trying to figure out how to concatenate the number pad's respective number to a number string whenever the button is pressed. I created a class called numpad that stores my number pad, and I got it to display on the GUI, but whenever I press a number on the number pad, nothing is printed in the console. Here is the code:

#!/usr/bin/python3

"""Pythulator Version 1.0.0"""

import math
import tkinter as tk
from decimal import Decimal, getcontext

numstring = ""

class Numpad:
    global numstring

    def __init__(self, master):
        num_frame = tk.Frame(master)
        num_frame.pack(side = tk.BOTTOM)
        self.numpad_1 = tk.Button(num_frame, text = "1", command = self.concat)
        self.numpad_1.grid(row = 6, column = 3)
        self.numpad_2 = tk.Button(num_frame, text = "2", command = self.concat)
        self.numpad_2.grid(row = 6, column = 4)
        self.numpad_3 = tk.Button(num_frame, text = "3", command = self.concat)
        self.numpad_3.grid(row = 6, column = 5)
        self.numpad_4 = tk.Button(num_frame, text = "4", command = self.concat)
        self.numpad_4.grid(row = 5, column = 3)
        self.numpad_5 = tk.Button(num_frame, text = "5", command = self.concat)
        self.numpad_5.grid(row = 5, column = 4)
        self.numpad_6 = tk.Button(num_frame, text = "6", command = self.concat)
        self.numpad_6.grid(row = 5, column = 5)
        self.numpad_7 = tk.Button(num_frame, text = "7", command = self.concat)
        self.numpad_7.grid(row = 4, column = 3)
        self.numpad_8 = tk.Button(num_frame, text = "8", command = self.concat)
        self.numpad_8.grid(row = 4, column = 4)
        self.numpad_9 = tk.Button(num_frame, text = "9", command = self.concat)
        self.numpad_9.grid(row = 4, column = 5)
        self.numpad_0 = tk.Button(num_frame, text = "0", command = self.concat)
        self.numpad_0.grid(row = 7, column = 4)

    def concat(self):
        self = str(self)
        numstring.join(self)
        print(numstring)



root = tk.Tk()

obj = Numpad(root)

root.geometry("400x400")
root.mainloop() 

我在想原因是当我按下一个数字键盘按钮时,控制台中没有打印任何内容,当它调用 concat 函数时,由于变量不是等价的,因此没有要连接的内容到一个字符串.无论如何我可以为每个按钮分配一个字符串来连接吗?我尝试在类中为每个数字键盘按钮创建一个函数,为其分配一个字符串,但这占用了太多空间,我只想用一两个函数来完成.

I'm thinking the reason nothing prints in the console when I press one of the number pad buttons, when it calls the concat function, there is nothing to concatenate since the variable isn't equivalent to a string. Is there anyway I could assign each button a string to concatenate? I tried creating a function in the class for each number pad button that assigns it a string, but that took up far too much space and I only want to do it with one or two functions.

推荐答案

这个方法问题很大:

def concat(self):
    self = str(self)
    numstring.join(self)
    print(numstring)

您还没有定义 __str__ 所以 str(self) 将是一些时髦的字符串.然后 numstring.join(self) 是一个无操作——它返回一个你根本没有分配给任何东西的字符串!所以你总是打印你开始时的空 numstring.

You have not defined __str__ so str(self) is going to be some funky string. Then numstring.join(self) is a no-operation -- it returns a string you're not assigning to anything at all! So you're always printing the empty numstring you started with.

而且——你没有考虑到哪个键被按下了...任何按键触发完全相同的self.concat打电话,没有关于又是哪个钥匙?"的信息.

And -- nowhere are you taking into account which key has been pressed... any keypress triggers exactly the same self.concat call, no info left about "which key was it again?".

functools.partial 允许您提前绑定参数(是的,您可以用一个杂乱无章的 lambda 来做,但是,您将很多strong> 如果你忘记了 lambda 的存在,你会更快乐...)

functools.partial lets you bind arguments in advance (yes, you could do with a bedraggled lambda, but, you'll be a much happier camper if you forget about lambda's existence...).

例如一个按钮应该是...:

So for example one button should be...:

self.numpad_1 = tk.Button(num_frame, text='1',
    command=functools.partial(self.concat, '1'))

其他人也一样.

现在,concat 方法将接收与被点击的按钮对应的文本,当然它需要将其记录在某处.

Now, the concat method will receive the text corresponding to the button that was clicked and of course it needs to record it somewhere.

我建议避免使用全局变量,而是使用

I'd recommend eschewing globals and instead starting the __init__ with

self.nums = []

现在 concat 过上了轻松的生活:

Now concat has an easy life:

def concat(self, digit):
    self.nums.append(digit)
    print(''.join(self.nums))

顺便说一句,这让您可以更轻松地实现关键功能,例如删除键——它只需要删除 self.nums 列表的最后一项(self.nums.pop() 就足够了),让用户更正错别字至关重要!

Incidentally, this lets you far more easily implement key features such as a delete key -- it just needs to drop the last item of the self.nums list (self.nums.pop() will suffice) and it's crucial to let the user correct a typo!

这篇关于如何根据调用它的 Button 执行更多命令?(类和 tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆