-2

我在 tkinter python 中做了一个计算器。当我想输入 21 时,由于数字输入到输入框中的方式,它会输入 12。我想纠正那个。

我已经尝试过 tag-right 但它不适用于条目标签。

""" 创建于 2019 年 9 月 19 日星期四 17:49:21

@作者:石田 """ x = 0

def add_one():
    if(x == 0):
        entry1.insert(0, "1")
    elif(x == 1):
        entry2.insert(0, "1")

.
.
.
.
def next_num():
    global x
    x = 1

def add_num():
    result["text"] = ""
    num1 = entry1.get()
    num2 = entry2.get()
    output = int(num1) + int(num2)
    result["text"] = str(output)
.
.
.
.
def clear_nums():
    entry1.delete(0, "end")
    entry2.delete(0, "end")
    result["text"] = ""
    global x
    x = 0

import tkinter as tk
from tkinter import *

calc = tk.Tk()
calc.title("Basic Calculator")
calc.geometry("300x600")
calc.config(background="white")

mainframe1 = tk.Frame(calc)
mainframe1.pack()

entry1 = tk.Entry(mainframe1, width=3, background="white", font="Times 20")
entry1.insert("end", " ")
entry1.configure(justify="right")
entry2 = tk.Entry(mainframe1, width=3, background="white", font="TImes 20")
entry2.insert("end", " ")
entry2.configure(justify="right")
result = tk.Label(mainframe1, width=3, font="Times 20")


button1 = tk.Button(mainframe1, height=2, width=4, background="white", text="1", font="Times 20", command=add_one)

calc.mainloop()

在输入 1 和 2 时,输入框应该给出 12,但它给出 21。数字输入与应用程序上的数字一起发生(而不是使用键盘)。

4

1 回答 1

0

如果要在条目的末尾插入值,则需要指定“end”(或END来自 tkinter 的常量)而不是零:

def add_one():
    if(x == 0):
        entry1.insert("end", "1")
    elif(x == 1):
        entry2.insert("end", "1")
于 2019-09-20T13:20:46.587 回答