how i can assign the result of the entry textbox to a variable to then pass into a function in the backend?









up vote
0
down vote

favorite












I'm having some issues programming a hangman game using tkinter, with the front-end and back-end separated AND both using OOP (i understand using OOP in the backend wasn't really necessary and it probably would've been better as a library but I'm doing this as a learning exercise) . I'm new to tkinter and i was wondering how i can assign the result of the entry textbox to a variable to then pass into a function in the backend. I was also wondering if my self.letter_row and self.guesses_left labels would then be updated as they are updated in the backend, and though i have assigned them to the front at the initiation of game_gui(), i'm not sure if they'll do this automatically. Here's all the frontend code...



from tkinter import *
from tkinter.ttk import *
import hangmanSkeleton as hm

class game_gui():

def __init__(self,master):
self.master = master
master.title("Hangman")

this_game = hm.Hangman()

self.word = this_game.word
self.letter_row = this_game.letter_row
self.letters_guessed = this_game.letters_guessed
self.guesses_left = this_game.guesses_left
self.letter = StringVar()

self.lbl_word = Label(master, text = "Welcome to Hangman!")
self.lbl_word.grid(row = 0,padx = 10 , pady = 15, columnspan = 2)

self.lbl_row = Label(master, text = self.letter_row)
self.lbl_row.grid(row = 1, column = 0, padx = 15, pady = 10)

self.lbl_guesses = Label(master, text = "Guesses Left: " + str(self.guesses_left))
self.lbl_guesses.grid(row = 1, column = 1, padx = 15, pady = 10)

self.entry_letter= Entry(master, textvariable = self.letter)
self.entry_letter.grid(row = 2, padx = 10, pady = 20, columnspan = 2)

self.guess_button = Button(master, text = "Guess Letter", command=lambda: this_game.guess_letter(self.letter))
self.guess_button.grid(row = 3, padx = 10, pady = 10, columnspan = 2)


root = Tk()
gui = game_gui(root)
root.mainloop()


And here's just the function from the backend that I'm trying to get to work in self.guess_button() above..



def guess_letter(self, letter): 
try:
if type(letter) != str or len(letter) != 1 or letter not in ascii_lowercase:
raise TypeError
if letter in self.letters_guessed:
raise ValueError
elif letter in self.word:
for count in range (len(self.word)):
if letter==self.word[count]:
self.letter_row = self.letter_row[0:count] + letter + self.letter_row[count+1:]
else:
pass
self.guesses_left = self.guesses_left-1 #take one away from guesses
except TypeError:
print("Value given is not a letter")
except ValueError:
print("Letter has already been guessed")


thank you!










share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm having some issues programming a hangman game using tkinter, with the front-end and back-end separated AND both using OOP (i understand using OOP in the backend wasn't really necessary and it probably would've been better as a library but I'm doing this as a learning exercise) . I'm new to tkinter and i was wondering how i can assign the result of the entry textbox to a variable to then pass into a function in the backend. I was also wondering if my self.letter_row and self.guesses_left labels would then be updated as they are updated in the backend, and though i have assigned them to the front at the initiation of game_gui(), i'm not sure if they'll do this automatically. Here's all the frontend code...



    from tkinter import *
    from tkinter.ttk import *
    import hangmanSkeleton as hm

    class game_gui():

    def __init__(self,master):
    self.master = master
    master.title("Hangman")

    this_game = hm.Hangman()

    self.word = this_game.word
    self.letter_row = this_game.letter_row
    self.letters_guessed = this_game.letters_guessed
    self.guesses_left = this_game.guesses_left
    self.letter = StringVar()

    self.lbl_word = Label(master, text = "Welcome to Hangman!")
    self.lbl_word.grid(row = 0,padx = 10 , pady = 15, columnspan = 2)

    self.lbl_row = Label(master, text = self.letter_row)
    self.lbl_row.grid(row = 1, column = 0, padx = 15, pady = 10)

    self.lbl_guesses = Label(master, text = "Guesses Left: " + str(self.guesses_left))
    self.lbl_guesses.grid(row = 1, column = 1, padx = 15, pady = 10)

    self.entry_letter= Entry(master, textvariable = self.letter)
    self.entry_letter.grid(row = 2, padx = 10, pady = 20, columnspan = 2)

    self.guess_button = Button(master, text = "Guess Letter", command=lambda: this_game.guess_letter(self.letter))
    self.guess_button.grid(row = 3, padx = 10, pady = 10, columnspan = 2)


    root = Tk()
    gui = game_gui(root)
    root.mainloop()


    And here's just the function from the backend that I'm trying to get to work in self.guess_button() above..



    def guess_letter(self, letter): 
    try:
    if type(letter) != str or len(letter) != 1 or letter not in ascii_lowercase:
    raise TypeError
    if letter in self.letters_guessed:
    raise ValueError
    elif letter in self.word:
    for count in range (len(self.word)):
    if letter==self.word[count]:
    self.letter_row = self.letter_row[0:count] + letter + self.letter_row[count+1:]
    else:
    pass
    self.guesses_left = self.guesses_left-1 #take one away from guesses
    except TypeError:
    print("Value given is not a letter")
    except ValueError:
    print("Letter has already been guessed")


    thank you!










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm having some issues programming a hangman game using tkinter, with the front-end and back-end separated AND both using OOP (i understand using OOP in the backend wasn't really necessary and it probably would've been better as a library but I'm doing this as a learning exercise) . I'm new to tkinter and i was wondering how i can assign the result of the entry textbox to a variable to then pass into a function in the backend. I was also wondering if my self.letter_row and self.guesses_left labels would then be updated as they are updated in the backend, and though i have assigned them to the front at the initiation of game_gui(), i'm not sure if they'll do this automatically. Here's all the frontend code...



      from tkinter import *
      from tkinter.ttk import *
      import hangmanSkeleton as hm

      class game_gui():

      def __init__(self,master):
      self.master = master
      master.title("Hangman")

      this_game = hm.Hangman()

      self.word = this_game.word
      self.letter_row = this_game.letter_row
      self.letters_guessed = this_game.letters_guessed
      self.guesses_left = this_game.guesses_left
      self.letter = StringVar()

      self.lbl_word = Label(master, text = "Welcome to Hangman!")
      self.lbl_word.grid(row = 0,padx = 10 , pady = 15, columnspan = 2)

      self.lbl_row = Label(master, text = self.letter_row)
      self.lbl_row.grid(row = 1, column = 0, padx = 15, pady = 10)

      self.lbl_guesses = Label(master, text = "Guesses Left: " + str(self.guesses_left))
      self.lbl_guesses.grid(row = 1, column = 1, padx = 15, pady = 10)

      self.entry_letter= Entry(master, textvariable = self.letter)
      self.entry_letter.grid(row = 2, padx = 10, pady = 20, columnspan = 2)

      self.guess_button = Button(master, text = "Guess Letter", command=lambda: this_game.guess_letter(self.letter))
      self.guess_button.grid(row = 3, padx = 10, pady = 10, columnspan = 2)


      root = Tk()
      gui = game_gui(root)
      root.mainloop()


      And here's just the function from the backend that I'm trying to get to work in self.guess_button() above..



      def guess_letter(self, letter): 
      try:
      if type(letter) != str or len(letter) != 1 or letter not in ascii_lowercase:
      raise TypeError
      if letter in self.letters_guessed:
      raise ValueError
      elif letter in self.word:
      for count in range (len(self.word)):
      if letter==self.word[count]:
      self.letter_row = self.letter_row[0:count] + letter + self.letter_row[count+1:]
      else:
      pass
      self.guesses_left = self.guesses_left-1 #take one away from guesses
      except TypeError:
      print("Value given is not a letter")
      except ValueError:
      print("Letter has already been guessed")


      thank you!










      share|improve this question















      I'm having some issues programming a hangman game using tkinter, with the front-end and back-end separated AND both using OOP (i understand using OOP in the backend wasn't really necessary and it probably would've been better as a library but I'm doing this as a learning exercise) . I'm new to tkinter and i was wondering how i can assign the result of the entry textbox to a variable to then pass into a function in the backend. I was also wondering if my self.letter_row and self.guesses_left labels would then be updated as they are updated in the backend, and though i have assigned them to the front at the initiation of game_gui(), i'm not sure if they'll do this automatically. Here's all the frontend code...



      from tkinter import *
      from tkinter.ttk import *
      import hangmanSkeleton as hm

      class game_gui():

      def __init__(self,master):
      self.master = master
      master.title("Hangman")

      this_game = hm.Hangman()

      self.word = this_game.word
      self.letter_row = this_game.letter_row
      self.letters_guessed = this_game.letters_guessed
      self.guesses_left = this_game.guesses_left
      self.letter = StringVar()

      self.lbl_word = Label(master, text = "Welcome to Hangman!")
      self.lbl_word.grid(row = 0,padx = 10 , pady = 15, columnspan = 2)

      self.lbl_row = Label(master, text = self.letter_row)
      self.lbl_row.grid(row = 1, column = 0, padx = 15, pady = 10)

      self.lbl_guesses = Label(master, text = "Guesses Left: " + str(self.guesses_left))
      self.lbl_guesses.grid(row = 1, column = 1, padx = 15, pady = 10)

      self.entry_letter= Entry(master, textvariable = self.letter)
      self.entry_letter.grid(row = 2, padx = 10, pady = 20, columnspan = 2)

      self.guess_button = Button(master, text = "Guess Letter", command=lambda: this_game.guess_letter(self.letter))
      self.guess_button.grid(row = 3, padx = 10, pady = 10, columnspan = 2)


      root = Tk()
      gui = game_gui(root)
      root.mainloop()


      And here's just the function from the backend that I'm trying to get to work in self.guess_button() above..



      def guess_letter(self, letter): 
      try:
      if type(letter) != str or len(letter) != 1 or letter not in ascii_lowercase:
      raise TypeError
      if letter in self.letters_guessed:
      raise ValueError
      elif letter in self.word:
      for count in range (len(self.word)):
      if letter==self.word[count]:
      self.letter_row = self.letter_row[0:count] + letter + self.letter_row[count+1:]
      else:
      pass
      self.guesses_left = self.guesses_left-1 #take one away from guesses
      except TypeError:
      print("Value given is not a letter")
      except ValueError:
      print("Letter has already been guessed")


      thank you!







      python oop tkinter widget tkinter-entry






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 23:17

























      asked Nov 10 at 21:57









      user6113119

      83




      83






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted











          Comment: I still can't get my labels self.letter_row and self.guesses_left to update though




          Yes, these variables are not bound to the Label, i recommend using Label.configure(text=... instead.
          Note: I show only .this_game.letter_row!



          Add a function update_labels(... and .configure(... the Labels text:



          def update_labels(self):
          self.lbl_row.configure(text=self.this_game.letter_row)
          # and so on


          Call this function at last in .__init__(... and .guess_letter(....

          The variable self.letter_row is not needed at all.





          Question: how i can assign the result of the entry textbox to a variable to then pass into a function in the backend.




          Change the following:




          • Make your variable this_game a member of class game_gui



            self.this_game = hm.Hangman()



          • Add a helper method guess_letter(... to class game_gui.

            Pass the letter to .this_game.guess_letter(....

            Clear the letter from .letter.



            def guess_letter(self, letter):
            self.this_game.guess_letter(letter.get())
            letter.set('')



          • Change the command, pointing to the helper method:



            ..., command=lambda: self.guess_letter(self.letter)






          share|improve this answer






















          • brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
            – user6113119
            Nov 10 at 23:17










          • also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
            – user6113119
            Nov 10 at 23:19










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243809%2fhow-i-can-assign-the-result-of-the-entry-textbox-to-a-variable-to-then-pass-into%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted











          Comment: I still can't get my labels self.letter_row and self.guesses_left to update though




          Yes, these variables are not bound to the Label, i recommend using Label.configure(text=... instead.
          Note: I show only .this_game.letter_row!



          Add a function update_labels(... and .configure(... the Labels text:



          def update_labels(self):
          self.lbl_row.configure(text=self.this_game.letter_row)
          # and so on


          Call this function at last in .__init__(... and .guess_letter(....

          The variable self.letter_row is not needed at all.





          Question: how i can assign the result of the entry textbox to a variable to then pass into a function in the backend.




          Change the following:




          • Make your variable this_game a member of class game_gui



            self.this_game = hm.Hangman()



          • Add a helper method guess_letter(... to class game_gui.

            Pass the letter to .this_game.guess_letter(....

            Clear the letter from .letter.



            def guess_letter(self, letter):
            self.this_game.guess_letter(letter.get())
            letter.set('')



          • Change the command, pointing to the helper method:



            ..., command=lambda: self.guess_letter(self.letter)






          share|improve this answer






















          • brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
            – user6113119
            Nov 10 at 23:17










          • also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
            – user6113119
            Nov 10 at 23:19














          up vote
          0
          down vote



          accepted











          Comment: I still can't get my labels self.letter_row and self.guesses_left to update though




          Yes, these variables are not bound to the Label, i recommend using Label.configure(text=... instead.
          Note: I show only .this_game.letter_row!



          Add a function update_labels(... and .configure(... the Labels text:



          def update_labels(self):
          self.lbl_row.configure(text=self.this_game.letter_row)
          # and so on


          Call this function at last in .__init__(... and .guess_letter(....

          The variable self.letter_row is not needed at all.





          Question: how i can assign the result of the entry textbox to a variable to then pass into a function in the backend.




          Change the following:




          • Make your variable this_game a member of class game_gui



            self.this_game = hm.Hangman()



          • Add a helper method guess_letter(... to class game_gui.

            Pass the letter to .this_game.guess_letter(....

            Clear the letter from .letter.



            def guess_letter(self, letter):
            self.this_game.guess_letter(letter.get())
            letter.set('')



          • Change the command, pointing to the helper method:



            ..., command=lambda: self.guess_letter(self.letter)






          share|improve this answer






















          • brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
            – user6113119
            Nov 10 at 23:17










          • also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
            – user6113119
            Nov 10 at 23:19












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted







          Comment: I still can't get my labels self.letter_row and self.guesses_left to update though




          Yes, these variables are not bound to the Label, i recommend using Label.configure(text=... instead.
          Note: I show only .this_game.letter_row!



          Add a function update_labels(... and .configure(... the Labels text:



          def update_labels(self):
          self.lbl_row.configure(text=self.this_game.letter_row)
          # and so on


          Call this function at last in .__init__(... and .guess_letter(....

          The variable self.letter_row is not needed at all.





          Question: how i can assign the result of the entry textbox to a variable to then pass into a function in the backend.




          Change the following:




          • Make your variable this_game a member of class game_gui



            self.this_game = hm.Hangman()



          • Add a helper method guess_letter(... to class game_gui.

            Pass the letter to .this_game.guess_letter(....

            Clear the letter from .letter.



            def guess_letter(self, letter):
            self.this_game.guess_letter(letter.get())
            letter.set('')



          • Change the command, pointing to the helper method:



            ..., command=lambda: self.guess_letter(self.letter)






          share|improve this answer















          Comment: I still can't get my labels self.letter_row and self.guesses_left to update though




          Yes, these variables are not bound to the Label, i recommend using Label.configure(text=... instead.
          Note: I show only .this_game.letter_row!



          Add a function update_labels(... and .configure(... the Labels text:



          def update_labels(self):
          self.lbl_row.configure(text=self.this_game.letter_row)
          # and so on


          Call this function at last in .__init__(... and .guess_letter(....

          The variable self.letter_row is not needed at all.





          Question: how i can assign the result of the entry textbox to a variable to then pass into a function in the backend.




          Change the following:




          • Make your variable this_game a member of class game_gui



            self.this_game = hm.Hangman()



          • Add a helper method guess_letter(... to class game_gui.

            Pass the letter to .this_game.guess_letter(....

            Clear the letter from .letter.



            def guess_letter(self, letter):
            self.this_game.guess_letter(letter.get())
            letter.set('')



          • Change the command, pointing to the helper method:



            ..., command=lambda: self.guess_letter(self.letter)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 11:36

























          answered Nov 10 at 22:52









          stovfl

          7,2733931




          7,2733931











          • brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
            – user6113119
            Nov 10 at 23:17










          • also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
            – user6113119
            Nov 10 at 23:19
















          • brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
            – user6113119
            Nov 10 at 23:17










          • also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
            – user6113119
            Nov 10 at 23:19















          brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
          – user6113119
          Nov 10 at 23:17




          brilliant! didn't know you could add this_game onto the object but that works so well. I still can't get my labels self.letter_row and self.guesses_left to update though - any advice on this?
          – user6113119
          Nov 10 at 23:17












          also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
          – user6113119
          Nov 10 at 23:19




          also strangely its raising the 'value given is not a letter' error but not the repeat letter one, not sure whats going on there cos its all working in the backend...
          – user6113119
          Nov 10 at 23:19

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243809%2fhow-i-can-assign-the-result-of-the-entry-textbox-to-a-variable-to-then-pass-into%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo