簡體   English   中英

如何在 Python 輸入字段中添加整數

[英]How to add Integers in a Python Entry Field

因此,在 Python 中創建 GUI 時,我是一個新手,我想知道如何在 state 在 ZA7F5F35426B627411FC9231B 中禁用 state 的輸入字段中添加值/整數我實際上在 Java 中做過同樣的事情,但我似乎無法弄清楚如何從 Java 轉換為 Python。

Java版

C_ID.setText(String.valueOf(Integer.parseInt(C_ID.getText())+1));

Python代碼

# Title for the Registration Form
label = Label(window, text="Customer Registration System", width=30, height=1, bg="yellow", anchor="center")
label.config(font=("Courier", 10))
label.grid(column=2, row=1)

# Customer ID Label (Left)
ID_Label = Label(window, text="Customer ID:", width=14, height=1, bg="yellow", anchor="w")
ID_Label.config(font=("Courier", 10))
ID_Label.grid(column=1, row=2)

# Customer ID Input Field
C_ID = StringVar()
C_ID = Entry(window, textvariable=C_ID, text="1")
C_ID.insert(0, "1")
C_ID.config(state=DISABLED)
C_ID.grid(column=2, row=2)

額外信息:每次按下保存按鈕時,我的代碼都需要增加 1。

def save():
    if len(C_Name.get()) == 0 or len(C_Email.get()) == 0 or len(C_Birthday.get()) == 0 or len(
            C_Address.get()) == 0 or len(C_Contact.get()) == 0:
        msg_box("All Input Fields Must Be Complete", "Record")
    elif not check_email:
        msg_box("Please Input a Valid Email", "Record")
    elif not check_dateValid:
        msg_box("Please Input a Valid date", "Record")
    elif not check_minor:
        msg_box("Minors are Not Allowed to Register", "Record")
    else:
        msg_box("Save Record", "Record")

我在這里嘗試使用此代碼C_ID.config(text=str(int(C_ID.get())+1))1

但無論我做什么,它似乎都沒有添加。

您在代碼中正確執行此操作。

您只需將配置設置為正常,設置文本,然后將配置設置回禁用。

C_ID.config(state=NORMAL) # sets config to normal
C_ID.delete(0, END) #deletes the current value in entry
C_ID.insert(0, "2")  # enters a new default value
C_ID.config(state=DISABLED) # sets config to disabled again

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM