【问题标题】:NameError: name 'Radiobutton' is not definedNameError:名称“单选按钮”未定义
【发布时间】:2017-09-17 19:47:01
【问题描述】:

我正在构建一个消息传递应用程序,并将 Tkinter 用于 GUI。我正在尝试创建一些按钮,但收到一条错误消息:

Traceback (most recent call last):

  File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 174, in <module>
    app = Application(the_window)

  File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 49, in __init__
    self.AES_radiobutton = Radiobutton(text = 'AES algorithm', bg = color, variable=self.var, value=0)
NameError: name 'Radiobutton' is not defined

我在这里发布所有源代码:

import tkinter as tk
from tkinter import IntVar

from tkinter import messagebox
from tkinter import Frame

from tkinter import Frame
from tkinter import Text
from tkinter import Label


from AESEncDec import *
from MD5Hashing import *
from RSAEncDec import *

color = 'lightblue' #color our background


class Application(Frame):

    def __init__(self, root=None):

        Frame.__init__(self, root)
        self.frame_width = 700
        self.frame_height = 400

        # Set configuration our frame
        self.config(width = self.frame_width, height = self.frame_height, bg = color) 
        self.pack()

        # Create textBox for input data
        self.textbox_one = Text()
        self.textbox_one.place(x = 30, y = 170, height = 200, width = 300 )

        # Create textBox for result
        self.textbox_two = Text()
        self.textbox_two.place(x = 370, y = 170, height = 200, width = 300 )

        label_input_text = Label( text = "Input text: ", bg = color)
        label_input_text.place(x = 30, y = 155, height = 10, width = 70 )

        label_output_text = Label( text = "Result: ", bg = color)
        label_output_text.place(x = 370, y = 155, height = 10, width = 50 )

        # IntVar help to detect, what radioButton was chosen
        self.var = IntVar()

        # Create radioButton for AES
        self.AES_radiobutton = Radiobutton(text = 'AES algorithm', bg = color, variable=self.var, value=0)
        self.AES_radiobutton.place(x = 100, y = 20, height = 30, width = 100 )

        # Create radioButton for DSA
        self.DSA_radiobutton = Radiobutton(text = 'DSA algorithm', bg = color, variable=self.var, value=1)
        self.DSA_radiobutton.place(x = 100, y = 70, height = 30, width = 100 )

        # Create radioButton for Hash function
        self.HF_radiobutton = Radiobutton(text = 'Hash function', bg = color, variable=self.var, value=2)
        self.HF_radiobutton.place(x = 100, y = 120, height = 30, width = 100 )

        # Create label
        self.lable_for_ask_bits = Label(text = 'Input size of bits:', bg = color)
        self.lable_for_ask_bits.place(x = 210, y = 70, height = 30, width = 100 )

        # Create textBox for input bits
        self.input_bits = Text()
        self.input_bits.place(x = 310, y = 75, height = 20, width = 50 )
        self.input_bits.insert(INSERT, '16')

        # Create button to encrypt text
        self.encrypt_button = Button(root, text = "Encrypt text", command = self.encrypt_text)
        self.encrypt_button.place(x = 420, y = 20, height = 80, width = 100 )

        # Create button to decrypt text
        self.decrypt_button = Button(root, text = "Decrypt text", command = self.decrypt_text)
        self.decrypt_button.place(x = 540, y = 20, height = 80, width = 100 )

        # Create button to hash
        self.hash_button = Button(root, text = "Hash text", command = self.hashing )
        self.hash_button.place(x = 420, y = 120, height = 30, width = 220)

        # Create AES object, keyword "this is a very strong key"
        # You can change keyword
        self.AES = AESEncDec('this is a very strong key')

        # Save bits
        self.bit_length = 16
        # Create RSA object
        self.RSA = RSAEncDec(self.bit_length)


    def encrypt_text(self):
        self.textbox_two.delete("1.0", END)
        # Get radioButton selection
        selection = self.var.get()
        # if chosen AES
        if selection == 0:
            # Read text from input
            message = self.textbox_one.get("1.0", END)
            encrypt_message = self.AES.encrypt(message)
            # Output result
            self.textbox_two.insert(INSERT, encrypt_message)
        # if chosen RSA
        elif selection == 1:
            try:
                # Read number of bits
                tmp_bits = int(self.input_bits.get("1.0", END))
                # if bits not in range from 4 to 32 print error message
                if tmp_bits < 4 or tmp_bits > 32:
                    tkMessageBox.showerror(message ='Bits must be in range from 4 to 32')
                else:
                    # else, if tmp_bits not = self.bit_length: create new object
                    if tmp_bits != self.bit_length:
                        self.bit_length = tmp_bits
                        self.RSA = RSAEncDec(self.bit_length)
            except:
                tkMessageBox.showerror(message ='You must input integer number')

            # Find max number
            max_number = self.RSA.get_max_value_to_encrypt()

            try:
                # Read text from input (myst be number)
                message = int(self.textbox_one.get("1.0", END))
                if message < 0 or message > max_number:
                    tkMessageBox.showerror(message ='Input text must be number in range from 0 to ' + str(max_number))
                else:
                    encrypt_message = self.RSA.encrypt(message)
                    # Output result
                    self.textbox_two.insert(INSERT, encrypt_message)
            except:
                tkMessageBox.showerror(message ='Input text must be number in range from 0 to ' + str(max_number))
        else:
            tkMessageBox.showinfo(message ='You must select "AES" or "RSA" radioButton')


    def decrypt_text(self):
        self.textbox_two.delete("1.0", END)
        # Get radioButton selection
        selection = self.var.get()
        # if chosen AES
        if selection == 0:
            # Read text from input
            message = self.textbox_one.get("1.0", END)
            decrypt_message = self.AES.decrypt(message)
            # Output result
            self.textbox_two.insert(INSERT, decrypt_message)
        elif selection == 1:
            # Read text from input
            message = int(self.textbox_one.get("1.0", END))
            decrypt_message = self.RSA.decrypt(message)
            # Output result
            self.textbox_two.insert(INSERT, decrypt_message)
        else:
            tkMessageBox.showinfo(message ='You must select "AES" or "RSA" radioButton')

    def hashing(self):
        # Get radioButton selection
        selection = self.var.get()
        # if chosen Hash function
        if selection == 2:
            # Read text from input
            message = self.textbox_one.get("1.0", END)
            # Hashing
            hashing_message = Hashing(message)
            # Output result
            self.textbox_two.insert(INSERT, hashing_message)
        else:
            tkMessageBox.showinfo(message ='You must select "Hash function" radioButton')


#create object TK class
the_window = tk.Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()

我仍然可以看到 GUI 和一个输入框,但是缺少很多元素。这里可能有什么问题?

【问题讨论】:

  • 你不需要从 tkinter 导入 RadiaButton 吗?!
  • 你已经发布了很多关于同一问题的问题,为什么不使用建议的语法来调用模块组件:tkinter.RadioButtontk.RadioButton 如果你定义了别名?
  • 最好指定我应该在什么地方调用它以及它应该是什么样子,因为我的尝试不起作用:(
  • 任何需要 RadioButton 的地方

标签: python user-interface tkinter nameerror


【解决方案1】:

Radiobutton 模块的简单导入解决了这个问题

【讨论】:

  • 您可以像这样从模块tkinter 中导入每个对象,但是如果您使用其中很多,导入语句将与您的代码一样长。您可以通过导入 tkinter 然后使用 tkinter.Labeltkinter.RadioButton 来避免所有这些导入。重要的是你需要告诉 Python 它在哪里可以找到你需要的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2021-04-15
  • 2019-01-26
  • 2021-10-05
  • 2017-08-16
相关资源
最近更新 更多