还有其他方法吗?在结果框中,我想将变量total转换为百分比。我试图将总变量变为百分比,但它被定义为全局变量。这是我的代码。请看标记框。我试图划分str(总计)/ 2 * 100但是可以' t。
import Tkinter
import tkMessageBox
total = 0
EasyBox1 = Tkinter.Tk()
EasyBox1.geometry("250x200")
EasyBox1.title("Quesion 1")
EasyBox2 = Tkinter.Tk()
EasyBox2.geometry("250x200")
EasyBox2.title("Quesion 2")
ResultBox = Tkinter.Tk()
ResultBox.geometry("320x260")
ResultBox.title("Results")
def next1():
global total
if not answr1.get(): tkMessageBox.showerror('no answer')
elif int(answr1.get()) == 8: total += 1
print "total after 1:",total
EasyBox1.withdraw()
EasyBox2.deiconify()
def mark():
global total
if not answr2.get(): tkMessageBox.showerror('no answer')
elif answr2.get() in ["B", "b"]: total += 1
print "total after 2:", total
EasyBox2.withdraw()
LabelName5 = Tkinter.Label(ResultBox, text="Marks : " + (str(total))/2*100, font=("Impact",20))
LabelName5.grid(row=2,column=0)
ResultBox.deiconify()
# window 2
Tkinter.Label (EasyBox1, text="answer:").pack()
answr1 = Tkinter.Entry (EasyBox1)
answr1.pack()
LabelName2 = Tkinter.Label (EasyBox1, text="State the number of edges in a cube")
LabelName2.grid(row=1,column=0)
LabelName2.pack()
# window 2
Tkinter.Label (EasyBox2, text="answer:").pack()
answr2 = Tkinter.Entry (EasyBox2)
answr2.pack()
LabelName2 = Tkinter.Label (EasyBox2, text="What is the place value of the digit 4 in 76421?")
LabelName2.grid(row=1,column=0)
LabelName2.pack()
LabelName2 = Tkinter.Label (EasyBox2, text="A.Thousands B.Hundreds C.Ones D.Tens")
LabelName2.grid(row=1,column=0)
LabelName2.pack()
# hide windows
EasyBox2.withdraw()
ResultBox.withdraw()
Tkinter.Button(EasyBox1, text="Next", command=next1).pack()
Tkinter.Button(EasyBox2, text="result", command=mark).pack()
EasyBox1.mainloop()
答案 0 :(得分:0)
您正在尝试将数字转换为字符串,然后将其除以100.您可以修改变量,无论它是本地变量还是全局变量。
我认为你应该使用格式字符串。 %%打印%符号。而%.2f将float(f)转换为带有2个小数位(.2)的字符串。
text="Marks : %.2f %%" % (total/2*100.0)