python 3.x - Label keeps on appearing -


so using python 3.4 , tkinter.

and when call function again n again contains label, label keeps on appearing in window previous label doesn't go away?

how can remove printed label gui window function called , display new one?

here code:-

#def prestart():     #here check if number of match okay, if not, user redirected setting else, call start()  def start():      #cpu choice     cpu_choice = label(historyframe, text = "cpu choosed: {}".format(dict['cpu_choice']))      #played match     #played_num_of_match = label(scoreframe, text = "number of matches played: {}".format(int(dict['match_played'])))      #display status     status_disp = label(scoreframe, text = "current status: {}".format(dict['status']))      if(int(dict['match_played']) < int(dict['num_of_match'])):         playframe.grid(row = 1, column = 0)         historyframe.grid(row = 2, column = 1)         status_disp.pack(fill=x)     elif(int(dict['match_played']) == int(dict['num_of_match'])):         playframe.grid(row = 1, column = 0)         historyframe.grid(row = 2, column = 1)         status_disp.pack(fill=x)         cp = dict['cpu_point']         = dict['user_point']         result(cp, up)     cpu_choice.pack(fill = x)     scoreframe.grid(row = 2, column = 0) 

this function updates display!

def send_value(x):     #here run logic of game , change value of key in dictionary , call start() @ end of change. 

now, choice buttons not in definition don't need called again n again. make playframe disappear n appear! here code them:-

#display question question = label(playframe, text = "rock? paper? scissor?")  #rock rock = button(playframe, text = "rock!", command = lambda: send_value("rock"))  #paper paper = button(playframe, text = "paper!", command = lambda: send_value("paper"))  #scissor scissor = button(playframe, text = "scissor!", command = lambda: send_value("scissor")) 

so when user clicks rock/paper/scissor, change key value in dictionary! if keep label outside function, doesn't auto updated!

everything else working perfectly. i'll kind of start make code cleaner.

try instead of creating new label every time:

import tkinter tk  class window():      def __init__(self, root):          self.frame = tk.frame(root)         self.frame.pack()          self.i = 0         self.labelvar = tk.stringvar()         self.labelvar.set("this first text: %d" %self.i)           self.label = tk.label(self.frame, text = self.labelvar.get(), textvariable = self.labelvar)         self.label.pack(side = tk.left)          self.button = tk.button(self.frame, text = "update", command = self.updatelabel)         self.button.pack(side = tk.right)      def updatelabel(self):          self.i += 1         self.labelvar.set("this new text: %d" %self.i)   root = tk.tk() window = window(root) root.mainloop() 

important points:

1) class used, easier pass values around when tkinter objects , variables member variables, accessible of gui functions.

2) updatelabel not create new label. updates stringvar() object hold new text every time call function. accomplished textvariable = self.labelvar keyword when creating label widget.

ps: done in python 2.5 code work you, change tkinter tkinter

edit 06/19/2015:

if want implement similar have code, without using class, you'll need pass around references variables.

1) change start:

your labels cpu_choice, status_disp, etc. should created outside of function; in same location question, rock, paper, scissors, etc. pack them outside of function well. same calls .grid inside of start; shouldn't need call pack or grid more once: right when create widget.

the following lines:

    playframe.grid(row = 1, column = 0)     historyframe.grid(row = 2, column = 1)     status_disp.pack(fill=x) 

can done outside of function well; execute these 3 statements under both if , elif conditions. means aren't conditional statements; done regardless of validity of condition.

2) create stringvar both cpu_choice & status_disp & edit labels follows (remember, outside of function):

    cpu_choice_text = stringvar()     cpu_choice_text.set("set whatever shown @ start of game")     cpu_choice = label(historyframe, text = cpu_choice_text.get(), textvariable = cpu_choice_text)     cpu_choice.pack(fill = x)      # , same thing status_disp 

3) when call start, pass cpu_choice_text & status_disp_text (or whatever called). instead of trying change text field of label frame, may use set call on stringvar connected label & label automatically update. example:

    def start(cpu_choice_text, status_disp_text):         cpu_choice.set(text = "cpu choice: {}".format(dict['cpu_choice']))         ... 

alternatively, wrap in class , make easier using self on every tkinter variable & widget. in way won't need pass variables functions, access member variables directly have self.i, self.labelvar in example.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -