Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone please help me?
#1
Why does it keep saying denied when I write "password" in the text box. I assume it has to do with the "content=" line, but for the life of me I have no idea what it should be.

from tkinter import *

class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        self.instruction = Label(self, text = "Enter PW")
        self.instruction.grid(row = 0, column=0, columnspan = 3, sticky = W)

        self.text = Text(self, width = 35, height =5, wrap = WORD)
        self.text.grid(row =1, column=1, sticky = W)

        self.submit_button = Button(self, text = "Submit", command = self.reveal)
        self.submit_button.grid(row = 2, column = 0, sticky =W)

        self.text1 = Text(self, width = 35, height =5, wrap = WORD)
        self.text1.grid(row = 3, column =0, columnspan = 2, sticky=W)
    def reveal (self):
        content = self.text.get("2.0", END)
        if content == "password":
            message = "congrats"
        else:
            message = "denied"
        self.text1.insert(0.0, message)
root = Tk()
root.title("password")
root.geometry("250x250")
app = Application(root)

root.mainloop()
Reply
#2
Forgive me for not running the code - I don't have time at the moment! But I would recommend you add
print("content is %r" % content)
line right after you define content in your reveal function and see what it says content is.
(I'd also recommend removing the space in
reveal (self):
;)
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#3
When I run it, it prints <<<content is 'assword\n'>>> - if I add a space in the input field before I type password, it then prints <<<content is 'password\n' ... Both still display "denied" in text box below. I'd really, really, really appreciate some further help!
Reply
#4
Try this
content = self.text.get("1.0","end-1c")
About this... the "2.0" was telling tkinter to grab the text starting with the second element (so your first letter, p, was dropped). text.get also appends a newline (the '\n' character) to the input, so the 'end-1c' removes that.

SeeĀ https://stackoverflow.com/questions/1482...t#14824164 for a better explanation. :)
Cheers!
Fred
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#5
We're on the rigth track, now it prints "assword" when I type "password" instead of "'assword/n" ---- Perhaps I need to adjust the one character somehow?
Reply
#6
What does the content line look like now?
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#7
GOT IT. THANKS!!!!
Reply
#8
Awesome! Take care and happy coding!
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#9
Can you possibly explain what the 1.0 means? the programs working, but Im still a bit confused. (I get that it's the position, of course, I just don't get why it needs realigning from 1 textbox to the other)
Reply
#10
Sure! The 1.0 tells the the self.text.get() function where to start on the word you've input. For 1.0 that means get the entire word starting at the first letter (so "password"). If you instead said 3.0 it wold start at the third letter and you'd have gotten "ssword". If 4.0 then you'd get "sword", and so on.

At least, that's what I read. :) I tend to use Qt for GUI programming so I'm not really familiar with Tkinter.
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply


Forum Jump:

User Panel Messages

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