Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

java - How to add Integers in a Python Entry Field

So I'm a newbie when it comes to creating GUI's in Python, and I was wondering how I can add values/integers in an Entry Field that has its state disabled in Python. I've actually done the same thing in Java, but I can't seem to figure out how to translate from Java to Python.

Java Version

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

Python Code

# 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)

Extra Info: My code needs to increment by 1 every time I press the save button.

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")

I tried using this code right here, C_ID.config(text=str(int(C_ID.get())+1))1

But it doesn't seem to add no matter what I do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are doing it correctly in your code.

You just need to set the config to normal, set the text, and then set the config back to disabled.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...