Python Forum
[Tkinter] Lambda seems to call expression 1 extra time every time it's called.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Lambda seems to call expression 1 extra time every time it's called.
#11
Move the player move button's connect out of the diceroll method, only connect once.
Reply
#12
(May-18-2019, 08:16 AM)Yoriz Wrote: Move the player move button's connect out of the diceroll method, only connect once.

I should have thought of that! That has almost fixed my problem, however, 'self.move_players' is called with the old value not the new one.
Here is my new 'diceRoll':
def diceRoll(self, argument):
        temp = 0
        rollT = Settings.ROLL_TIME
        self.roll.setEnabled(False)
        while(temp <= rollT):
            yield ((temp * 0.02) + 0.05)
            img = random.choice(self.images)
            spaces = int(img[0:1])
            pixmap = QPixmap(img)
            self.image.setPixmap(pixmap)
            self.vbox2.addWidget(self.image)
            temp += 1
        self.move.setEnabled(True)
        print(spaces)
        a = lambda: self.move_players("P1" if self.currPl == 1 else "P2", spaces)
        if(self.notCalled):
            self.move.clicked.connect(a)
            self.notCalled = False
When player 1 rolls and they get a 4, for instance, it 'calls self.move_players' with the arguments of 'P1' and 4. Then player 2 rolls a 6 and 'self.move_players' is called withe the arguments 'P2' and 4. Not 6, 4. Even though spaces equals 6, it still uses player 1's value of 4. This is similar to what I described in my original post.
With all the prints, here is the wrong output:
Output:
1 P1 1 3 P2 1
and here is the output I should get
Output:
1 P1 1 3 P2 3
Just a slight difference.

D

EDIT: after rolling the dice more that two times, I can see that for every player, it uses the first value that was rolled. For instance, if player 1 rolls a 5 on the first go, every player, every go will move 5 spaces, no matter what they roll.
Reply
#13
The self.move.clicked.connect doesn't need to be in diceRoll at all, place it in the setup code not the event code, just like you have self.roll.clicked.connect
players does not need passing to the method self.move_players it can access the current player directly from self.currPl
spaces can be set as an instance variable in diceRoll, self.move_players can then access the current spaces directly from self.spaces

Remove the lambda, remove the parameters from self.move_players and connect directly to self.move_players.
It will now be connected with no out of date arguments as it gets the current values itself at the point the button is clicked.
Reply
#14
Finally! I did what you suggested and moved it out of the diceRoll function and into the dice function, and also use a variable self.spaces, again, like you suggested, to update the players position.

Thank you all for the help!
Dream
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Doubt approach update 2 Treeview at the same time TomasSanchexx 7 1,865 Sep-19-2023, 01:19 AM
Last Post: SaintOtis12
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,273 Aug-11-2022, 06:25 AM
Last Post: chinky
  tkinter get method is not accepting value when called by function jagasrik 1 2,497 Sep-16-2020, 05:28 AM
Last Post: Yoriz
  How to stop time counter in Tkinter LoneStar 1 4,298 Sep-11-2020, 08:56 PM
Last Post: Yoriz
  Display text 3 words at a time algae 5 2,706 Jun-27-2020, 10:25 AM
Last Post: menator01
Photo Visualizing time series data of graph nodes in plotly deepa 3 3,450 Mar-16-2020, 07:06 AM
Last Post: Larz60+
  [Tkinter] How to adjust time - tkinter Ondrej 2 2,811 Jun-20-2019, 05:53 PM
Last Post: Yoriz
  [Tkinter] Unable to create checkbox and select at run time tej7gandhi 5 4,587 May-05-2019, 04:57 PM
Last Post: tej7gandhi
  Buttons not appearing, first time button making admiral_hawk 5 3,333 Dec-31-2018, 03:26 AM
Last Post: admiral_hawk
  clear all widgets at same time (not delete/remove) shift838 0 2,717 Dec-17-2018, 11:55 PM
Last Post: shift838

Forum Jump:

User Panel Messages

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