Python: How to get an entry box within a message box? -


i realised hadn't asked user name @ start of game. tried code:

 label(root, text="what name?").grid(row=0, column=0)  e1 = self.entry(root)  e1.self.grid(row = 0, column=1) 

but not working.

how message box pop in start of game saying "welcome math bubbles, name?" user types in name , presses ok. message box pop saying "hello _, press start button begin". save users name in winner message box display name well

there no messageboxes have entry. name suggests, messageboxes meant show messages, not receive input or act mini gui's.

nevertheless, quite possible want. there many ways well. think best this:

from tkinter import * import random tkinter.messagebox import showinfo  class bubbleframe:     ##############################################     def __init__(self, root, name):         self.name = name     ###############################################         root.title("math bubbles")         self.bubbles = {}         self.score = 0         button(root, text="start", width=8, command=self.initialize_bubbles).pack()         button(root, text="quit", width=8, command=quit).pack()         self.canvas = canvas(root, width=800, height=650, bg='#afeeee')         self.canvas.create_text(400, 30, fill="darkblue", font="times 20 italic bold", text="click bubbles multiples of two.")         self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="times 15 italic bold", text="your score is: 0")         self.canvas.pack()      def initialize_bubbles(self):         each_no in range(1, 21):             xval = random.randint(5, 765)             yval = random.randint(5, 615)             oval_id = self.canvas.create_oval(xval, yval, xval + 30, yval + 30,fill="#00ffff", outline="#00bfff", width=5, tags="bubble")             text_id = self.canvas.create_text(xval + 15, yval + 15, text=each_no, tags="bubble")             self.canvas.tag_bind("bubble", "<button-1>", lambda x: self.click(x))             self.bubbles[oval_id] = (xval, yval, 0, 0, each_no, text_id)      def click(self, event):         if self.canvas.find_withtag(current):             item_uid = event.widget.find_closest(event.x, event.y)[0]             is_even = false             try:                 self.bubbles[item_uid]             except keyerror:                 key, value in self.bubbles.iteritems():                     if item_uid == value[5]:                         if value[4] % 2 == 0:                             is_even = true                         self.canvas.delete(key)                         self.canvas.delete(item_uid)             else:                 if self.bubbles[item_uid][4] % 2 == 0:                     is_even = true                 self.canvas.delete(item_uid)                 self.canvas.delete(self.bubbles[item_uid][5])             if is_even:                 self.score += 1             else:                 self.score -= 1              if self.score == 10:                 #########################################                 showinfo("winner", "you won %s!" % self.name)                 #########################################          self.canvas.delete(self.current_score)         self.current_score = self.canvas.create_text(200, 60, fill="darkblue", font="times 15 italic bold", text="your score is: %s"%self.score)      def loop(self, root):         oval_id, (x, y, dx, dy, each_no, text_id) in self.bubbles.items():             dx += random.randint(-1, 1)             dy += random.randint(-1, 1)             dx, dy = max(-5, min(dx, 5)), max(-5, min(dy, 5))             if not 0 < x < 770:                 dx = -dx             if not 0 < y < 620:                 dy = -dy             self.canvas.move(oval_id, dx, dy)             self.canvas.move(text_id, dx, dy)             self.bubbles[oval_id] = (x + dx, y + dy, dx, dy, each_no, text_id)         root.after(100, self.loop, root)  if __name__ == "__main__":      root = tk()     ###############################################################     label(root, text="welcome math bubbles, name?").pack()     name = entry(root)     name.pack()     def submit(name, root):         root.destroy()         root = tk()         label(root, text="hello %s, press start button begin.\n" % name).pack()         bubbleframe(root, name).loop(root)     button(root, text="ok", command=lambda: submit(name.get(), root)).pack()     ######################################################################     root.mainloop() 

the code inside comment boxes changed. can see, approach create little gui @ startup asks name. once gets it, sends main application , self-destructs.

furthermore, adding argument name __init__ method of bubbleframe means can do:

 self.name = name 

and have user's name attribute of bubbleframe. also, note did not use toplevel window. because, if launched toplevel @ startup, generate blank main window, , looks bad. approach clean , closest want.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -