【问题标题】:Quiz Mark Convert into percentage测验分数转换成百分比
【发布时间】:2016-05-29 23:53:43
【问题描述】:

还有其他方法吗?在结果框中,我想将变量总计转换为百分比。我试图将总变量设为百分比,但它被定义为全局变量。这是我的代码。请看标记框。我试图划分 str(total)/2*100 但不能。

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

【问题讨论】:

  • @WoLy 我需要你的帮助。你能帮帮我吗?
  • @adrianus 你有什么想法吗?

标签: python python-2.7 user-interface python-3.x button


【解决方案1】:

您试图将一个数字转换为一个字符串,然后将其除以 100。您可以修改一个变量,无论它是局部变量还是全局变量。

我认为您应该使用格式字符串。 %% 打印 % 符号。而 %.2f 将浮点数 (f) 转换为具有 2 个小数位 (.2) 的字符串。

text="Marks : %.2f %%" % (total/2*100.0) 

【讨论】:

  • 仍然无法正常工作。错误是无法签名到函数调用
  • 错误是无法签名到函数调用。我已经改成你给我的文本代码了
  • 我的意思是,您能否提供准确说明错误所在行的输出?我以前从未听说过这个错误。
  • 我将此代码 LabelName5 = Tkinter.Label(ResultBox, text="Marks : " + (str(total))/2*100, font=("Impact",20)) 更改为你给我的代码。
  • 我明白这一点。有一个输出说明错误和发生错误的行。我想看到描述代码为什么不起作用的输出。错误的描述很模糊,我们需要看看到底是什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多