将变量与.get()比较的问题

时间:2019-04-24 19:27:42

标签: python python-3.x variables tkinter

我有一个问题,我不知道如何将变量与条目的.get()进行比较,这是什么问题?

我是python新手,我是法语

我的代码:

from tkinter import *
from random import *
from tkinter.messagebox import *

liste = ["A TI TA", "B TA TI TI TI", "C TA TI TA TI", "D TA TI TI", "E TI", "F TI TI TA TI", "G TA TA TI", "H TI TI TI TI", "I TI TI", "J TI TA TA TA", "K TA TI TA", "L TI TA TI TI", "M TA TA", "N TA TI", "O TA TA TA", "P TI TA TA TI", "Q TA TA TI TA", "R TI TA TI", "S TI TI TI", "T TA", "U TI TI TA", "V TI TI TI TA", "W TI TA TA", "X TA TI TI TA", "Y TA TI TA TA", "Z TA TA TI TI"]

class Interface(Frame):
    def __init__(self, fenetre, **kwargs):

    def cliquer():
        lettrer = choice(liste)
        self.lettre["text"] = lettrer

    def verification():
        if saisie.get() == (lettrer):
            showinfo("Saisie correcte", "Saisie correcte")
        else:
            showinfo("Erreur", "ERREUR")

    Frame.__init__(self, fenetre, width=768, height=576, **kwargs)
    self.pack(fill=BOTH)


    self.message2 = Label(self, text="APRENDRE LE CODE MORSE")
    self.message2.pack(side="top")

    self.message = Label(self, text="Cliquez sur générer")
    self.message.pack()

    self.lettre = Label(self, text="")
    self.lettre.pack()

    saisie= StringVar()
    self.champ = Entry(self, textvariable = saisie, bg = "bisque", fg = "maroon")
    self.champ.focus_set()
    self.champ.pack()

    self.bouton_quitter = Button(self, text="Quitter", command=self.quit)
    self.bouton_quitter.pack(side="left")

    self.bouton_cliquer = Button(self, text="Generer", textvariable = "lettrer", command = cliquer)
    self.bouton_cliquer.pack(side="right")

    self.confirmbouton = Button(self, text="Valider", command = verification)
    self.confirmbouton.pack()


fenetre = Tk()
interface = Interface(fenetre)

interface.mainloop()
interface.destroy()

按“验证器”时出现我的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\albru\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/albru/.PyCharmCE2018.3/config/scratches/interface.py", line 15, in verification
    if saisie.get() == (lettrer):
NameError: name 'lettrer' is not defined

ZeEleveZozo

4 个答案:

答案 0 :(得分:0)

该功能范围内没有字母,您需要使用self.lettrer。因此,将“ if saisie.get()==(lettrer):”更改为“ if saisie.get()==(self.lettrer):”。

答案 1 :(得分:0)

此时:

if saisie.get() == (lettrer):
            showinfo("Saisie correcte", "Saisie correcte")

lettrer未定义。为什么?因为lettrer不是全局变量。它是Interface的实例变量。为了表明您想将saisie.get()与实例变量lettrer进行比较,您需要像这样将self.放在前面:

if saisie.get() == self.lettrer:  # I removed the redundant parantheses
            showinfo("Saisie correcte", "Saisie correcte")

答案 2 :(得分:0)

当您将oops与tkinter一起使用时,请尝试使用selfself.saisie = StringVar())来使类中的每个变量成为全局变量,特别是在不同函数中使用该变量时。

在内部定义函数也不是坏方法,但如果在类外部使用self定义函数,则效果会更好。

如果在按下“ Quitter”按钮之前先按下“ Generer”按钮,则会产生错误,这是因为“ letterer”变量仅在您按下“ Quitter”时才定义,因此您可以在__init__中对其进行定义设为self.lettrer = None,或者只是通过self.lettrer = choice(liste)给出第一个随机选择。这样可以避免出现错误:AttributeError: 'Interface' object has no attribute 'lettrer'

这是更新的代码:

from tkinter import *
from random import *
from tkinter.messagebox import *

liste = ["A TI TA", "B TA TI TI TI", "C TA TI TA TI", "D TA TI TI", "E TI", "F TI TI TA TI", "G TA TA TI", "H TI TI TI TI", "I TI TI", "J TI TA TA TA", "K TA TI TA", "L TI TA TI TI", "M TA TA", "N TA TI", "O TA TA TA", "P TI TA TA TI", "Q TA TA TI TA", "R TI TA TI", "S TI TI TI", "T TA", "U TI TI TA", "V TI TI TI TA", "W TI TA TA", "X TA TI TI TA", "Y TA TI TA TA", "Z TA TA TI TI"]

class Interface(Frame):
    def __init__(self, fenetre, **kwargs):

        Frame.__init__(self, fenetre, width=768, height=576, **kwargs)
        self.pack(fill=BOTH)

        self.lettrer = choice(liste)
        self.message2 = Label(self, text="APRENDRE LE CODE MORSE")
        self.message2.pack(side="top")

        self.message = Label(self, text="Cliquez sur générer")
        self.message.pack()

        self.lettre = Label(self, text="")
        self.lettre.pack()

        self.saisie= StringVar()
        self.champ = Entry(self, textvariable = self.saisie, bg = "bisque", fg = "maroon")
        self.champ.focus_set()
        self.champ.pack()

        self.bouton_quitter = Button(self, text="Quitter", command=self.quit)
        self.bouton_quitter.pack(side="left")

        self.bouton_cliquer = Button(self, text="Generer", textvariable = "lettrer", command = self.cliquer)
        self.bouton_cliquer.pack(side="right")

        self.confirmbouton = Button(self, text="Valider", command = self.verification)
        self.confirmbouton.pack()

    def cliquer(self):
        self.lettrer = choice(liste)
        self.lettre["text"] = self.lettrer

    def verification(self):
        if self.saisie.get() == self.lettrer:
            showinfo("Saisie correcte", "Saisie correcte")
        else:
            showinfo("Erreur", "ERREUR")


fenetre = Tk()
interface = Interface(fenetre)

interface.mainloop()
interface.destroy()

希望对您有帮助。

答案 3 :(得分:0)

在我的版本下面,如果还不晚;)

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import random


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.saisie = tk.StringVar()
        self.letre = tk.StringVar()
        self.liste = ["A TI TA", "B TA TI TI TI", "C TA TI TA TI",
                      "D TA TI TI", "E TI", "F TI TI TA TI", "G TA TA TI",
                      "H TI TI TI TI", "I TI TI", "J TI TA TA TA",
                      "K TA TI TA", "L TI TA TI TI", "M TA TA", "N TA TI",
                      "O TA TA TA", "P TI TA TA TI", "Q TA TA TI TA",
                      "R TI TA TI", "S TI TI TI", "T TA", "U TI TI TA",
                      "V TI TI TI TA", "W TI TA TA", "X TA TI TI TA",
                      "Y TA TI TA TA", "Z TA TA TI TI"]

        self.init_ui()

    def init_ui(self):

        self.pack(fill=tk.BOTH, expand=1)

        f = ttk.Frame()
        ttk.Label(f, text = "APRENDRE LE CODE MORSE").pack()
        ttk.Label(f, text = "Cliquez sur générer").pack()
        ttk.Label(f, textvariable = self.letre).pack()
        self.champ = tk.Entry(f,
                              textvariable = self.saisie,
                              bg = "bisque",
                              fg = "maroon")
        self.champ.pack()
        self.champ.focus_set()

        w = ttk.Frame()

        ttk.Button(w, text="Generer",command=self.cliquer).pack()
        ttk.Button(w, text="Valider", command=self.verification).pack()
        ttk.Button(w, text="Quitter", command=self.on_close).pack()


        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)
        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)


    def cliquer(self,):
        lettrer = random.choice(self.liste)
        self.letre.set(lettrer)

    def verification(self):
        if self.saisie.get():

            if self.saisie.get() == (self.letre.get()):
                msg ="Saisie correcte"
            else:
                msg="Erreur"

            messagebox.showinfo(self.parent.title, msg)
        else:
            messagebox.showwarning(self.parent.title, 'Please fill saisie field')

    def on_close(self):
        self.parent.on_exit()


class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)

        self.set_style()
        self.set_title() 
        Main(self,)     

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")

    def set_title(self):
        s = "{0}".format('MORSE')
        self.title(s)

    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel( self.title(), "Do you want to quit?", parent=self):
            self.destroy()               

if __name__ == '__main__':
    app = App()
    app.mainloop()

https://docs.databricks.com/user-guide/secrets/example-secret-workflow.html#example-secret-workflow