李伦奥 发表于 2022-2-15 17:35:45

为什么这段代码在我这一直报错啊?求解(新手)

# -*- coding: utf-8 -*-

from Tkinter import *

master = Tk()

frame = Frame(master)
frame.pack(padx=10,pady=10)

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

def test(content):
    return content.isdigit()   

testCMD = master.register(test)

e1 = Entry(frame,textvariable=v1,validate="key",validatecommand=(testCMD,"%P")).grid(row=0,column=0)
Label(frame,text="+").grid(row=0,column=1)
e2 = Entry(frame,textvariable=v2,validate="key",validatecommand=(testCMD,"%P")).grid(row=0,column=2)
Label(frame,text="=").grid(row=0,column=3)
e3 = Entry(frame,textvariable=v3,state="readonly").grid(row=0,column=4)

def calc():
    result = int(v1.get())+int(v2.get())
    v3.set(str(result))
   
Button(frame,text="计算结果",command=calc).grid(row=1,column=2,pady=5)

mainloop()

isdkz 发表于 2022-2-15 17:46:46

因为Tkinter是python2的名字,python3请用tkinter

from tkinter import *                              # Tkinter改成tkinter

master = Tk()

frame = Frame(master)
frame.pack(padx=10,pady=10)

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

def test(content):
    return content.isdigit()   

testCMD = master.register(test)

e1 = Entry(frame,textvariable=v1,validate="key",validatecommand=(testCMD,"%P")).grid(row=0,column=0)
Label(frame,text="+").grid(row=0,column=1)
e2 = Entry(frame,textvariable=v2,validate="key",validatecommand=(testCMD,"%P")).grid(row=0,column=2)
Label(frame,text="=").grid(row=0,column=3)
e3 = Entry(frame,textvariable=v3,state="readonly").grid(row=0,column=4)

def calc():
    result = int(v1.get())+int(v2.get())
    v3.set(str(result))
   
Button(frame,text="计算结果",command=calc).grid(row=1,column=2,pady=5)

mainloop()

李伦奥 发表于 2022-2-15 17:51:34

isdkz 发表于 2022-2-15 17:46
因为Tkinter是python2的名字,python3请用tkinter

修改了以后还是报错啊:Traceback (most recent call last):
File ", line 3, in <module>
    from Tkinter import *
ImportError: No module named 'Tkinter'
>>>

李伦奥 发表于 2022-2-15 17:52:38

isdkz 发表于 2022-2-15 17:46
因为Tkinter是python2的名字,python3请用tkinter

修好啦,谢谢大神
页: [1]
查看完整版本: 为什么这段代码在我这一直报错啊?求解(新手)