小编jxs*_*shu的帖子

如何修改/添加文本到 tkinter.Label?

我正在学习基础Python。我目前正在尝试创建一个简单的计算器程序,只有加法和减法。不过我有一个问题。我不确定如何在按下按钮时将文本添加到 Python 标签中。现在,按下“1”按钮后,我的程序会将显示标签更改为文本“1”。但是,我希望我的程序添加文本,而不是设置文本。

例如,如果我按“按钮 1”5 次,它当前将重置标签文本 5 次,并生成一个 1。我希望它在按下时将数字添加到标签中,而不是替换。

按按钮 5 次后的当前结果:1
按按钮 5 次后请求的结果:11111

这是我当前的程序代码。如果有什么不清楚的地方,尽管问;谢谢。

from tkinter import *

window = Tk()

# Creating main label
display = Label(window, text="")
display.grid(row=0, columnspan=3)

def add_one():
    display.config(text='1')

# Creating all number buttons
one = Button(window, text="1", height=10, width=10, command=add_one)
two = Button(window, text="2", height=10, width=10)
three = Button(window, text="3", height=10, width=10)
four = Button(window, text="4", height=10, width=10)
five = Button(window, text="5", height=10, width=10)
six = Button(window, text="6", height=10, width=10) …
Run Code Online (Sandbox Code Playgroud)

python user-interface tkinter calculator

6
推荐指数
1
解决办法
2万
查看次数

我如何加快这个计划?

我目前正在尝试解决ProjectEuler问题,除了速度之外,我已经解决了所有问题.我几乎可以肯定程序执行速度这么慢的原因是由于嵌套循环.我会喜欢一些关于如何提高速度的建议.我是一名新手程序员,所以我不熟悉很多更高级的方法/主题.

public class Problem12 {

    public static void main(String[] args) {
        int num;

        for (int i = 1; i < 15000; i++) {
            num = i * (i + 1) / 2;
            int counter = 0;

            for (int x = 1; x <= num; x++) {
                if (num % x == 0) {
                    counter++;
                }
            }
            System.out.println("[" + i + "] - " + num + " is divisible by " + counter + " numbers."); …
Run Code Online (Sandbox Code Playgroud)

java performance loops nested-loops

5
推荐指数
1
解决办法
127
查看次数