Tkinter:字符串变量不起作用

时间:2013-05-21 01:37:45

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

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

这是我的代码,

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

提前致谢!

3 个答案:

答案 0 :(得分:3)

而不是'g == Hydrogen' or 'hydrogen' or 'HYDROGEN',您可以g.lower() == '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))

答案 1 :(得分:1)

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

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

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

但写起来更容易:

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

答案 2 :(得分:1)

顺便说一下,你可以摆脱所有这些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()来获取所有原子符号的列表。