开发者

Tkinter - Why is my background/frame not re-painting?

I am not sure if I am on the right track here-- but I started GUI programming with Python.

I have all of my buttons and entries worked out. The main problem I am having is with the method that rolls my die and places the result.

def roll(self):
    self.die = Die(int(self.sides.get(开发者_运维百科))) # gets from label
    t = self.die.roll()
    t += int(self.mod.get()) # gets from label
    self.result = Label(self.root, text=t).grid(row=2, column=1, sticky=W)

Is my problem the fact that I am re-instantiating a Label over the old one? Shouldn't the old Label's text be destroyed and the frame should only show the new label in its place?


It seems to me that you're not using objects at their best values. You should modify you code in this way:

  • each time you need a new roll, you instantiate a new Die. Why not keeping the same instance?
  • each time you want to display the roll, you instantiate a new Label. Maybe you're not aware of this, but you can update the label text (and any Tkinter widget), using its configure() method. This would mean that you need to grid the instance only the first time.

By the way, .grid returns None. If you want to keep reference of the result label, you have to use two lines for Label instantiation:

self.result = Label(self.root, text=t)  # first creating instance...
self.result.grid(row=2, colum=1, sticky=W)  # ... and placing it in self.root

Try to update your code like this. You will certainly feel the need to move some of this code to the __init__() function of self, so write it in your question as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜