Python Forum
'NoneType' object has no attribute 'get'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NoneType' object has no attribute 'get'
#8
Better style demo. Class usage plus real encryption.
import tkinter as tk  # Do not use wildcard import
from tkinter import ttk
from string import ascii_letters


class Enigma(tk.Tk):
    """Super secret message encrytion machine."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.style = ttk.Style(self)
        themes = self.style.theme_names()
        self.style.theme_use(themes[0])
        self.plain = tk.StringVar(self, "Message...")
        self.encrypted = tk.StringVar(self, "")
        self.theme = tk.StringVar(self, themes[0])

        # Root window does not get styled.  Fill root window with ttk frame
        top_frame = ttk.Frame(self)
        top_frame.pack(expand=True, fill=tk.BOTH)

        ttk.Label(top_frame, text="Encrypted").grid(
            row=0, column=0, padx=5, pady=5, sticky="e"
        )
        ttk.Label(top_frame, text="Decrpyted").grid(
            row=1, column=0, padx=5, pady=5, sticky="e"
        )
        ttk.Label(top_frame, text="Theme").grid(
            row=2, column=0, padx=5, pady=5, sticky="e"
        )

        ttk.Entry(top_frame, textvariable=self.plain, width=30).grid(
            row=0, column=1, padx=5, pady=5, sticky="w"
        )
        ttk.Entry(top_frame, textvariable=self.encrypted, width=30).grid(
            row=1, column=1, padx=5, pady=5, sticky="w"
        )

        combobox = ttk.Combobox(top_frame, textvariable=self.theme, values=themes)
        combobox.grid(row=2, column=1, padx=5, pady=5, sticky="w")
        combobox.bind("<<ComboboxSelected>>", self.set_theme)

        # Want different grid for buttons.
        button_frame = ttk.Frame(top_frame)
        button_frame.grid(row=4, column=0, columnspan=2)
        ttk.Button(button_frame, text="Encrypt", command=self.encrypt).grid(
            row=0, column=0, padx=5, pady=5
        )
        ttk.Button(button_frame, text="Decrypt", command=self.decrypt).grid(
            row=0, column=1, padx=5, pady=5
        )

    def _secret_cipher(self, message, shift):
        """Top secret encription technique."""
        letters = []
        for letter in message:
            if letter in ascii_letters:
                index = (ascii_letters.index(letter) + shift) % len(ascii_letters)
                letters.append(ascii_letters[index])
            else:
                letters.append(letter)
        return "".join(letters)

    def encrypt(self):
        """Apply secret cipher to message."""
        self.encrypted.set(self._secret_cipher(self.plain.get(), 4))

    def decrypt(self):
        """Decyphter encripted message."""
        self.plain.set(self._secret_cipher(self.encrypted.get(), -4))

    def set_theme(self, event):
        """Change window theme to selected."""
        self.style.theme_use(self.theme.get())


Enigma().mainloop()
Reply


Messages In This Thread
RE: 'NoneType' object has no attribute 'get' - by deanhystad - Oct-13-2023, 06:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,825 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,529 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,774 Aug-30-2022, 08:44 AM
Last Post: tompranks
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 16,000 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,819 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 15,085 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' linuxhacker 7 7,100 Aug-08-2020, 12:47 AM
Last Post: linuxhacker
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,424 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,399 Dec-20-2019, 09:52 PM
Last Post: joe_momma
  [Tkinter] AttributeError: 'App' object has no attribute 'set_text' Sahil1313 6 12,324 Jun-17-2018, 05:01 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020