【问题标题】:Tkinter: String variables not working [closed]Tkinter:字符串变量不起作用[关闭]
【发布时间】:2013-05-21 01:37:45
【问题描述】:

我制作了一个科学程序,它会询问你 n 元素的符号,你必须输入元素,但问题是它无法判断你的答案是否错误,它只会一直说正确!,

这是我的代码,

# Import Modules
from tkinter import *
import random, time

# Window Setup
root = Tk()
root.title('Element Finder')
root.state('zoomed')

# Elements

"""

a = H, Hydrogen

b = He, Helium

c = O, Oxygen

d = C, Carbon

e = N, Nitrogen

f = Ne, Neon

g = U, Uranium

h = Na, Sodium

i = Ar, Argon

j = K, Potassium

k = Mg, Magnesium

l = Cl, Chlorine

m = B, Boron

n = Au, Gold

o = Pd, Palladium

p = Kr, Krypton

q = Ca, Calcium

r = Pm, Promethium

s = Ag, Silver

t = Rb, Rubidium

u = Y, Yurium

v = Ir, Iridium

w = Pt, Platnium

x = Ti, Titanium

y = Hg, Mercury

z = Er, Erbium

"""

# Variables
element = StringVar()
guess = StringVar()
answer = StringVar()

def question():
    guess.set('')
    element.set('')
    r = random.randint(1, 26)
    if r == 1:
        element.set('H')
    elif r == 2:
         element.set('He')
    elif r == 3:
        element.set('O')
    elif r == 4:
        element.set('C')
    elif r == 5:
        element.set('Ne')
    elif r == 6:
        element.set('U')
    elif r == 7:
        element.set('Na')
    elif r == 8:
        element.set('Ar')
    elif r == 9:
        element.set('K')
    elif r == 10:
        element.set('Mg')
    elif r == 11:
        element.set('Cl')
    elif r == 12:
        element.set('B')
    elif r == 13:
        element.set('Au')
    elif r == 14:
        element.set('Pd')
    elif r == 15:
        element.set('Kr')
    elif r == 16:
        element.set('Ca')
    elif r == 17:
        element.set('Pm')
    elif r == 18:
        element.set('Ag')
    elif r == 19:
        element.set('Rb')
    elif r == 20:
        element.set('Y')
    elif r == 21:
        element.set('Ir')
    elif r == 22:
        element.set('Pt')
    elif r == 23:
        element.set('Ti')
    elif r == 24:
        element.set('N')
    elif r == 25:
        element.set('Hg')
    elif r == 26:
        element.set('Er')

def check():
    e = element.get()
    g = guess.get()
    if e == 'H' and g == 'Hydrogen' or 'hydrogen' or 'HYDROGEN':
        answer.set('Correct!')
    if e == 'He' and g == 'Helium' or 'helium' or 'HELIUM':
        answer.set('Correct')
    if e == 'O' and g == 'Oxygen' or 'oxygen' or 'OXYGEN':
        answer.set('Correct!')
    if e == 'C' and g == 'Carbon' or 'carbon' or 'CARBON':
        answer.set('Correct!')
    if e == 'Ne' and 'Neon' or 'neon' or 'NEON':
        answer.set('Correct!')
    if e == 'U' and g == 'Uranium' or 'uranium' or 'URANIUM':
        answer.set('Correct!')
    if e == 'Na' and g == 'Sodium' or 'sodium' or 'SODIUM':
        answer.set('Correct!')
    if e == 'Ar' and g == 'Argon, argon' 'ARGON':
        answer.set('Correct!')
    if e == 'K' and g == 'Potassium' or 'potassium' or 'POTASSIUM':
        answer.set('Correct!')
    if e == 'Mg' and g == 'Magnesium' or 'magnesium' or 'MAGNESIUM':
        answer.set('Correct!')
    if e == 'Cl' and g == 'Chlorine' or 'chlorine' or 'CHLORINE':
        answer.set('Correct!')
    if e == 'B' and g == 'Boron' or 'boron' or 'BORON':
        answer.set('Correct!')
    if e == 'Au' and g == 'Gold' or 'gold' or 'GOLD':
        answer.set('Correct!')
    if e == 'Pd' and g == 'Palladium' or 'palladium' or 'PALLADIUM':
        answer.set('Correct!')
    if e == 'Kr' and g == 'Krypton' or 'krypton' or 'KRYPTON':
        answer.set('Correct!')
    if e == 'Ca' and g == 'Calcium' or 'calcium' or 'CALCIUM':
        answer.set('Correct!')
    if e == 'Pm' and g == 'Promethium' or 'promethium' or 'PROMETHIUM':
        answer.set('Correct!')
    if e == 'Ag' and g == 'Silver' or 'silver' or 'SILVER':
        answer.set('Correct!')
    if e == 'Rb' and g == 'Rubidium' or 'rubidium' or 'RUBIDIUM':
        answer.set('Correct!')
    if e == 'Y' and g == 'Yurium' or 'yurium' or 'YURIUM':
        answer.set('Correct!')
    if e == 'Ir' and g == 'Iridium' or 'iridium' or 'IRIDIUM':
        answer.set('Correct!')
    if e == 'Pt' and g == 'Platnium' or 'platnium' or 'PLATNIUM':
        answer.set('Correct!')
    if e == 'Ti' and g == 'Titanium' or 'titanium' or 'TITANIUM':
        answer.set('Correct!')
    if e == 'N' and g == 'Nitrogen' or 'nitrogen' or 'NITROGEN':
        answer.set(g + ' is correct')
    if e == 'Hg' and g == 'Mercury' or 'mercury' or ' MERCURY':
        answer.set('Correct!')
    if e == 'Er' and g == 'Erbium' or 'erbium' or 'ERBIUM':
        answer.set('Correct!')
    else:
        answer.set('Incorrect.')
    question()

# Question
Label(root, text='What element has the symbol:', fg='black', font='20').pack(side=TOP)
e = Label(root, textvariable=element, fg='blue', font='20').pack(side=TOP)
a = Entry(root, textvariable=guess, font=20).pack(side=TOP)
Button(root, text='Check', fg='green', command=check).pack(side=TOP)
c = Label(root, textvariable=answer, font=20, fg='blue').pack(side=TOP)
question()

提前致谢!

【问题讨论】:

  • 不是“Yurium”,而是“Yttrium”……
  • 让我们看看: 1) 使用 lower() 将选择的数量减少到 1 是一个从未对任何其他学习 Python 的人有用的技巧。 2) 将一系列 if 语句转换为字典查找将永远不会帮助任何其他学习 python 的人。 A.Roda、Inbar Rose、Frank van Puffelen、plaes 和 Lichtamberg,请禁止自己完全无能。

标签: python string python-3.x tkinter tk


【解决方案1】:

你可以用g.lower() == 'hydrogen'代替'g == Hydrogen' or 'hydrogen' or 'HYDROGEN'

这里的问题是:

if g == 'Hydrogen' or 'hydrogen' or 'HYDROGEN'

评估为

 if (g == 'Hydrogen') or ('hydrogen') or 'HYDROGEN'

表达式的后半部分始终计算为True,因此出现错误。

您可以通过创建字典来大大简化代码:

d = {1: 'H', 2: 'He', ... }

并将if 的大块替换为element.set(d.get(r))

【讨论】:

  • 这很有帮助,但我不知道如何制作字典。
  • @user2318122, my_dict = {"a": 1, "b": 2, "c": 3};打印 my_dict["a"]。尝试一下。字典中的每个条目都有一个“键”和一个“值”,它们用冒号分隔。每个条目都用逗号分隔。很简单。
【解决方案2】:
g == 'Hydrogen' or 'hydrogen' or 'HYDROGEN'

所有这些陈述都是错误的。你必须写:

g == "Hydrogen" or g == "hydrogen" or g == "HYDROGEN"

但是写起来比较容易:

g.lower() == "hydrogen"   #added '()' per Blender's comment

【讨论】:

    【解决方案3】:

    顺便说一句,您可以像这样摆脱所有这些 if 语句:

    import random as r
    
    atomic_symbols = ["H", "He", "O"]
    rnum = r.randint(0, len(atomic_symbols) - 1 )
    element.set(atomic_symbols[rnum])
    

    还有:

    answers = {
        "H": "hydrogen", 
        "He": "helium", 
        "O": "oxygen"
    }
    
    if guess.get().lower() == answers[element.get()]:
        answer.set("Correct")
    

    另一方面,您必须创建一个包含所有值的大列表和大字典。实际上,要节省一些输入,只需创建字典,然后使用 keys() 获取所有原子符号的列表。

    【讨论】:

    • 非常感谢。我刚试过这个,效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 2014-11-16
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多