且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中使用tkinter使用GUI对计算器进行编程?

更新时间:2023-11-25 20:20:46

所以,我会尽我所能解释您给出的代码。 Calc()类包含这段代码的所有功能。该结构意味着主GUI(稍后设置)可以轻松访问每个功能。在 Calc()类中,您可以使用函数(用 def 等表示)。这些包含了此代码计算其输出所用的各种方法。

So, I'll explain the code you've given as best I understand it. The class Calc() contains all the functions for this piece of code. The structure means the main GUI (set up later) can access each function easily. Within the Calc() class, you have your functions (denoted by def etc.). These contain the various methods by which this code computes it's output.

在类之外,我们还有Tkinter UI代码。这段代码将在您的窗口中建立各种按钮和显示。在这种情况下,按钮和文本字段的位置由网格方法控制。如您所见,每次代码设置一个对象(此处仅 Frame Button 条目对象),有一个关联的 .grid(row = x,column = y ... etc)。这指定了UI中每个对象的相对位置。例如,使用网格方法,您可以通过给第一个对象row = 1,column = 0,第二行= 2,column = 0等来堆叠两个对象。

Outside of the class, we have the Tkinter UI code. This code builds your window inside of which your various buttons and displays sit. The positioning of the buttons and text fields in this case is governed by the 'grid' method. As you can see, every time the code sets up an object (here only Frame, Button and Entry objects), there is an associated .grid(row=x, column=y...etc). This specifies the relative positions of each object in the UI. For example, using the grid method you could stack two objects by giving the first object row=1, column=0 and the second row=2, column=0 etc.

for循环:

for j in range(1,4):
    for k in range(3):
        bttn.append(Button(calc, text = numbers[i]))
        bttn[i].grid(row = j, column = k, pady = 5)
        bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x)
        i += 1 

可能是UI中唯一无法直接看到您刚刚开始的部分。本质上,它所做的就是自动构建按钮(节省您分别编码每个按钮的时间)。
循环的前两行(希望)显然是在指定范围内的值上循环。在 bttn.append(... 这行下面,在较早建立的calc框架中创建一个按钮对象calc = Frame(root),按钮文本由数字列表(从上面的字面意义 numbers = 789456123 开始),最初,i = 0,所以numbers [i]将返回列表数字中的第一个元素,即numbers [i ]代表i = 1(下次循环)为8,依此类推。 bttn.grid(row = j,column = k,pady = 5)使用前面提到的网格定位方法,但是这里的j和k由循环本身中的值给定(对于行,值为1到4;对于列为0到2-3列;对于列, )。如果您运行代码,则可以看到将放置所有用于输入数字的键盘按钮的位置。此循环的最后一行(除了i + = 1,即向i加1)将为按钮分配命令。命令将调用关联的函数,在本例中为 Calc()中的 numpress 函数。您也许可以看到, numpress 本身会使用您按的任何数字来更新显示。

is probably the only part of the UI that is not straightforward to see if you're just starting out. In essence all it is doing is building buttons automatically (saving you the time to code each one individually). The first two lines in the loop are (hopefully) obviously looping over values in specified ranges. Beneath that the line bttn.append(... creates a button object in the calc frame set up earlier calc = Frame(root), with the button text being given by the list of numbers (literally numbers="789456123" above). Initially, i = 0, so numbers[i] would return the first element in the list numbers, numbers[i] for i=1 (the next time it loops over) would give 8 and so on. The bttn.grid(row = j, column = k, pady = 5) uses the grid positioning method mentioned earlier, but here the j and k are given by the values in the loop itself (values between 1 and 4 in the case of rows, and values between 0 and 2 - 3 columns - in the case of columns). If you run the code, you can see that this will position all the keypad buttons for inputting numbers. The last line in this loop (besides i+=1, i.e add 1 to i), handles assigning the button a command. The command will call an associated function, in this case the numpress function in Calc(). As you may be able to see, numpress itself updates your display with whatever number you press.

最后一个此示例中的代码处理了计算器的其余操作,您将注意到每个操作都遵循上述创建按钮,分配命令并将按钮放置在UI中的模式。通过上面的解释,您也许可以看到 Calc()中的每个其余函数都可以控制算术运算或清除等。

The final block of code in this example handles the remaining operations for the calculator, and you will notice that each also follows the above pattern of creating a button, assigning a command and positioning the button in the UI. With what I've explained above, you may be able to see that each of the remaining functions in Calc() governs an arithmetical operation, or clear etc.

我意识到这是一堵文字墙,但希望对您有所帮助!如果我不清楚或有什么特别不清楚的地方,请告诉我,我会尽力加以解释(我已经很久没学会了!)。

I realise this is a wall of text, but I hope it helps! If I've been unclear or there is anything in particular you don't understand, let me know, I'll try and explain it (I've not long learnt myself!).

您可能会发现此 Tkinter UI指南很有用,我从该指南中学到了很多基础知识。

You might find this Tkinter UI Guide useful, I learnt a lot of the basics from that guide.

祝你好运