Posts: 5
Threads: 2
Joined: Dec 2021
I try to make a simple program. Up button count the number and show the result on screen. Same with the down button. My problem is that my program count them in the background and shows only the last result (1000). Any idea how to say to my program "On every step show the new number in label"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from tkinter import *
root = Tk()
number = 0
label = Label (root, text = number).grid(row = 0 , column = 1 )
def up():
number = 0
while number < 1000 :
number = number + 1
print (number)
label = Label (root, text = number).grid(row = 0 , column = 1 )
button1 = Button(root, text = "UP" , command = up)
button2 = Button(root, text = "DOWN" )
button3 = Button(root, text = "QUIT" , command = quit)
button1.grid(row = 1 , column = 0 )
button2.grid(row = 1 , column = 1 )
button3.grid(row = 1 , column = 2 )
root.mainloop()
|
Posts: 1,145
Threads: 114
Joined: Sep 2019
You're kinds vague in how you want it done. Do you want the number to change on each click or do you want it to show a count up to 1000?
If by each click just update the label with each button click. If you want it to run up to 1000, have a look at the after method.
Posts: 5
Threads: 2
Joined: Dec 2021
Dec-28-2021, 02:17 PM
(This post was last modified: Dec-28-2021, 02:18 PM by George87.)
I want the label to be updated by each click but i don't know how to do it...
Posts: 1,145
Threads: 114
Joined: Sep 2019
One way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import tkinter as tk
import sys
class Window:
def __init__( self , parent):
self .counter = 0
container = tk.Frame(parent)
container.grid(column = 0 , row = 0 , sticky = 'new' )
self .label = tk.Label(container)
self .label[ 'text' ] = 'Counter: 0'
self .label.grid(column = 0 , row = 0 , sticky = 'new' )
btnframe = tk.Frame(parent)
btnframe.grid(column = 0 , row = 1 , sticky = 'new' )
for i in range ( 3 ):
btnframe.grid_columnconfigure(i, weight = 3 , uniform = 'btns' )
btn1 = tk.Button(btnframe, text = 'Up' , command = self .up)
btn1.grid(column = 0 , row = 1 , sticky = 'new' )
btn2 = tk.Button(btnframe, text = 'Down' , command = self .down)
btn2.grid(column = 1 , row = 1 , sticky = 'new' )
btn3 = tk.Button(btnframe, text = 'Exit' , command = sys.exit)
btn3.grid(column = 2 , row = 1 , sticky = 'new' )
def up( self ):
self .counter + = 1
self .label[ 'text' ] = f 'Counter: {self.counter}'
def down( self ):
if self .counter < 1 :
self .counter = 0
else :
self .counter - = 1
self .label[ 'text' ] = f 'Counter: {self.counter}'
def main():
root = tk.Tk()
root[ 'padx' ] = 5
root[ 'pady' ] = 3
Window(root)
root.mainloop()
main()
|
Posts: 1,145
Threads: 114
Joined: Sep 2019
Dec-28-2021, 04:00 PM
(This post was last modified: Dec-28-2021, 05:49 PM by menator01.)
Another way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import tkinter as tk
import sys
def doit(action):
doit.counter = getattr (doit, 'counter' , 0 )
if action = = 'add' :
doit.counter + = 1
elif action = = 'sub' :
doit.counter - = 1
if doit.counter < = 0 :
doit.counter = 0
else :
doit.counter = 0
label[ 'text' ] = f 'Counter: {doit.counter}'
app = tk.Tk()
app[ 'padx' ] = 5
app[ 'pady' ] = 3
label = tk.Label(app, text = 'Counter: 0' , anchor = 'w' )
label.pack(fill = 'x' )
btn1 = tk.Button(app, text = 'Up' , command = lambda : doit(action = 'add' ))
btn1.pack(side = 'left' )
btn2 = tk.Button(app, text = 'Down' , command = lambda : doit(action = 'sub' ))
btn2.pack(side = 'left' )
btn3 = tk.Button(app, text = 'Reset' , command = lambda : doit(action = 'reset' ))
btn3.pack(side = 'left' )
btn4 = tk.Button(app, text = 'Exit' , command = sys.exit)
btn4.pack(side = 'left' )
app.mainloop()
|
Posts: 5
Threads: 2
Joined: Dec 2021
I have very little python knowledge. Now I try to learn. I try too hard to understand what exactly you are doing but the second way seems easier  Is any easier way with less complex code closer to my code to understand what is missing? In any case thanks for helping me!
Posts: 6,798
Threads: 20
Joined: Feb 2020
To periodically change the count number you need something that runs periodically. Your approach failed because your up() function is called once instead of once for each number.
tkinter windows have a method .after(msec, func) that will call a function after some time (measured in milliseconds) expires. This can be used to repeatedly call a function to update your counter label.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import sys
import tkinter as tk
increment = 0
end_count = 10
def startCounter(start, end, incr):
global increment, end_count
counter. set (start)
increment = incr
end_count = end
def update():
if increment > 0 :
count = min (end_count, counter.get() + increment)
else :
count = max (end_count, counter.get() + increment)
counter. set (count)
root.after( 1000 , update)
root = tk.Tk()
counter = tk.IntVar(root, 0 )
tk.Label(root, textvariable = counter, width = 10 ).pack()
tk.Button(root, text = 'Up' , width = 10 , command = lambda : startCounter( 0 , 10 , 1 )).pack()
tk.Button(root, text = 'Down' , width = 10 , command = lambda : startCounter( 10 , 0 , - 1 )).pack()
tk.Button(root, text = 'Exit' , width = 10 , command = sys.exit).pack()
update()
root.mainloop()
|
Posts: 1,145
Threads: 114
Joined: Sep 2019
Dec-28-2021, 06:04 PM
(This post was last modified: Dec-28-2021, 06:04 PM by menator01.)
I updated the code with some comments. Maybe it will help you understand what takes place.
On a side note, it's best not to use wildcard imports e.g. from tkinter import *
This will cause you problems down the road
Posts: 6,798
Threads: 20
Joined: Feb 2020
I added in some poker rules to identify different hands. Still having a problem with two pair.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import collections
import copy
import random
class Card():
suit_names = [ "Clubs" , "Diamonds" , "Hearts" , "Spades" ]
short_suit_names = [ "C" , "D" , "H" , "S" ]
rank_names = [" ", " Ace ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 ", " 10 ", " Jack ", " Queen ", " King ", " Ace"]
short_rank_names = [" ", " A ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 ", " 10 ", " J ", " Q ", " K ", " A"]
rank_range = range ( 2 , 15 )
def __init__( self , rank, suit = None ):
if suit is None :
suit = Card.suit_names[Card.short_suit_names.index(rank[ - 1 ])]
rank = Card.short_rank_names[ 2 :].index(rank[: - 1 ]) + 2
self .rank = rank
self .suit = suit
def __lt__( self , other):
return self .rank < other.rank
def __gt__( self , other):
return self .rank > other.rank
def __eq__( self , other):
return self .rank = = other.rank
def name( self ):
return f "{self.rank_names[self.rank]} {self.suit}"
def __repr__( self ):
return f "{self.short_rank_names[self.rank]}{self.suit[0]}"
class Hand():
def __init__( self , cards = None ):
self .cards = [] if cards is None else cards
def ranks( self ):
def sort_key(item):
return ( len (item), item[ 0 ].rank)
ranks = {}
for card in self .cards:
if card.rank in ranks:
ranks[card.rank].append(card)
else :
ranks[card.rank] = [card]
ranks = list (ranks.values())
ranks.sort(key = sort_key, reverse = True )
return ranks
def suits( self ):
suits = {}
for card in self .cards:
if card.suit in suits:
suits[card.suit].append(card)
else :
suits[card.suit] = [card]
suits = list (suits.values())
for suit in suits:
suit.sort(reverse = True )
suits.sort(key = len , reverse = True )
return suits
def __add__( self , other):
return Hand( self .cards + other.cards)
def __repr__( self ):
return ", " .join([ str (card) for card in self .cards])
class Deck():
def __init__( self , shuffle = False ):
self .cards = [Card(rank, suit) for rank in Card.rank_range for suit in Card.suit_names]
if shuffle:
random.shuffle( self .cards)
def __len__( self ):
return len ( self .cards)
def deal( self , count):
cards = self .cards[:count]
self .cards = self .cards[count:]
return cards
class Rules():
ROYAL_FLUSH = 9
STRAIGHT_FLUSH = 8
FOUR_OF_A_KIND = 7
FULL_HOUSE = 6
FLUSH = 5
STRAIGHT = 4
THREE_OF_A_KIND = 3
TWO_PAIR = 2
ONE_PAIR = 1
HIGH_CARD = 0
HAND_NAMES = [
"High Card" ,
"One Pair" ,
"Two Pair" ,
"Three of a Kind" ,
"Straight" ,
"Flush" ,
"Full House" ,
"Four of a Kind" ,
"Straight Flush" ,
"Royal Flush"
]
@staticmethod
def ofAKind(ranks, count):
for rank in ranks:
if len (rank) = = count:
return rank
return None
@staticmethod
def flush(suits):
return suits[ 0 ] if len (suits[ 0 ]) > 4 else None
@staticmethod
def straight(ranks):
def sort_key(item):
return item[ 0 ].rank
straight = None
ranks.sort(key = sort_key, reverse = True )
if ranks[ 0 ][ 0 ].rank = = 14 :
ranks.append([Card( 1 , card.suit) for card in ranks[ 0 ]])
for rank in ranks:
if straight is None or straight[ - 1 ][ 0 ].rank - rank[ 0 ].rank > 1 :
straight = [rank]
else :
straight.append(rank)
if len (straight) > = 5 :
return straight
return None
@staticmethod
def straightFlush(suits):
if (cards : = Rules.flush(suits)) is not None :
return Rules.straight(Hand(cards).ranks())
return None
@staticmethod
def royalFlush(suits):
if (cards : = Rules.straightFlush(suits)) is not None and cards[ 0 ][ 0 ].rank = = 14 :
return cards
return None
@staticmethod
def fullHouse(ranks):
if len (ranks) > 1 and len (ranks[ 0 ]) > = 3 and len (ranks[ 1 ]) > = 2 :
return ranks[ 0 ] + ranks[ 1 ]
return None
@staticmethod
def twoPair(ranks):
if len (ranks) > 1 and len (ranks[ 0 ]) > = 2 and len (ranks[ 1 ]) > = 2 :
return ranks[ 0 ] + ranks[ 1 ]
return None
@staticmethod
def highCard(ranks):
def sort_key(item):
return item[ 0 ].rank
ranks.sort(key = sort_key, reverse = True )
return ranks[ 0 ][ 0 ].rank
@classmethod
def evaluate( cls , hand):
ranks = hand.ranks()
suits = hand.suits()
high_card = cls .highCard(ranks)
if (cards : = cls .royalFlush(suits)):
return cls .ROYAL_FLUSH, cards, high_card
elif (cards : = cls .straightFlush(suits)):
return cls .STRAIGHT_FLUSH, cards, high_card
elif (cards : = cls .ofAKind(ranks, 4 )):
return cls .FOUR_OF_A_KIND, cards, high_card
elif (cards : = cls .fullHouse(ranks)):
return cls .FULL_HOUSE, cards, high_card
elif (cards : = cls .flush(suits)):
return cls .FLUSH, cards, high_card
elif (cards : = cls .straight(ranks)):
return cls .STRAIGHT, cards, high_card
elif (cards : = cls .ofAKind(ranks, 3 )):
return cls .THREE_OF_A_KIND, cards, high_card
elif (cards : = cls .twoPair(ranks)):
return cls .TWO_PAIR, cards, high_card
elif (cards : = cls .ofAKind(ranks, 2 )):
return cls .ONE_PAIR, cards, high_card
else :
return cls .HIGH_CARD, high_card, high_card
flop = Hand([Card(card) for card in [ "KS" , "QS" , "JS" ]])
print ( "Royal Flush" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "AS" , "10S" ]]) + flop))
print ( "Straight Flush" , Rules.evaluate(Hand([Card(card) for card in [ "KC" , "KD" , "JH" , "10S" , "9S" ]]) + flop))
print ( "Four of a Kind" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "AS" , "10H" ]]) + flop))
print ( "Full House" , Rules.evaluate(Hand([Card(card) for card in [ "KC" , "KD" , "QH" , "5S" , "4H" ]]) + flop))
print ( "Flush" , Rules.evaluate(Hand([Card(card) for card in [ "2S" , "4S" , "2C" , "4C" , "5S" ]]) + flop))
print ( "Straight" , Rules.evaluate(Hand([Card(card) for card in [ "AD" , "2D" , "3H" , "4C" , "5C" ]]) + flop))
print ( "Three of a kind" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "5H" , "3D" ]]) + flop))
print ( "Two Pair" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "2S" , "2D" , "3D" ]]) + flop))
print ( "One Pair" , Rules.evaluate(Hand([Card(card) for card in [ "AS" , "AD" , "3H" , "4C" , "5C" ]]) + flop))
|
Posts: 1,145
Threads: 114
Joined: Sep 2019
(Dec-28-2021, 08:58 PM)deanhystad Wrote: I added in some poker rules to identify different hands. Still having a problem with two pair.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import collections
import copy
import random
class Card():
suit_names = [ "Clubs" , "Diamonds" , "Hearts" , "Spades" ]
short_suit_names = [ "C" , "D" , "H" , "S" ]
rank_names = [" ", " Ace ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 ", " 10 ", " Jack ", " Queen ", " King ", " Ace"]
short_rank_names = [" ", " A ", " 2 ", " 3 ", " 4 ", " 5 ", " 6 ", " 7 ", " 8 ", " 9 ", " 10 ", " J ", " Q ", " K ", " A"]
rank_range = range ( 2 , 15 )
def __init__( self , rank, suit = None ):
if suit is None :
suit = Card.suit_names[Card.short_suit_names.index(rank[ - 1 ])]
rank = Card.short_rank_names[ 2 :].index(rank[: - 1 ]) + 2
self .rank = rank
self .suit = suit
def __lt__( self , other):
return self .rank < other.rank
def __gt__( self , other):
return self .rank > other.rank
def __eq__( self , other):
return self .rank = = other.rank
def name( self ):
return f "{self.rank_names[self.rank]} {self.suit}"
def __repr__( self ):
return f "{self.short_rank_names[self.rank]}{self.suit[0]}"
class Hand():
def __init__( self , cards = None ):
self .cards = [] if cards is None else cards
def ranks( self ):
def sort_key(item):
return ( len (item), item[ 0 ].rank)
ranks = {}
for card in self .cards:
if card.rank in ranks:
ranks[card.rank].append(card)
else :
ranks[card.rank] = [card]
ranks = list (ranks.values())
ranks.sort(key = sort_key, reverse = True )
return ranks
def suits( self ):
suits = {}
for card in self .cards:
if card.suit in suits:
suits[card.suit].append(card)
else :
suits[card.suit] = [card]
suits = list (suits.values())
for suit in suits:
suit.sort(reverse = True )
suits.sort(key = len , reverse = True )
return suits
def __add__( self , other):
return Hand( self .cards + other.cards)
def __repr__( self ):
return ", " .join([ str (card) for card in self .cards])
class Deck():
def __init__( self , shuffle = False ):
self .cards = [Card(rank, suit) for rank in Card.rank_range for suit in Card.suit_names]
if shuffle:
random.shuffle( self .cards)
def __len__( self ):
return len ( self .cards)
def deal( self , count):
cards = self .cards[:count]
self .cards = self .cards[count:]
return cards
class Rules():
ROYAL_FLUSH = 9
STRAIGHT_FLUSH = 8
FOUR_OF_A_KIND = 7
FULL_HOUSE = 6
FLUSH = 5
STRAIGHT = 4
THREE_OF_A_KIND = 3
TWO_PAIR = 2
ONE_PAIR = 1
HIGH_CARD = 0
HAND_NAMES = [
"High Card" ,
"One Pair" ,
"Two Pair" ,
"Three of a Kind" ,
"Straight" ,
"Flush" ,
"Full House" ,
"Four of a Kind" ,
"Straight Flush" ,
"Royal Flush"
]
@staticmethod
def ofAKind(ranks, count):
for rank in ranks:
if len (rank) = = count:
return rank
return None
@staticmethod
def flush(suits):
return suits[ 0 ] if len (suits[ 0 ]) > 4 else None
@staticmethod
def straight(ranks):
def sort_key(item):
return item[ 0 ].rank
straight = None
ranks.sort(key = sort_key, reverse = True )
if ranks[ 0 ][ 0 ].rank = = 14 :
ranks.append([Card( 1 , card.suit) for card in ranks[ 0 ]])
for rank in ranks:
if straight is None or straight[ - 1 ][ 0 ].rank - rank[ 0 ].rank > 1 :
straight = [rank]
else :
straight.append(rank)
if len (straight) > = 5 :
return straight
return None
@staticmethod
def straightFlush(suits):
if (cards : = Rules.flush(suits)) is not None :
return Rules.straight(Hand(cards).ranks())
return None
@staticmethod
def royalFlush(suits):
if (cards : = Rules.straightFlush(suits)) is not None and cards[ 0 ][ 0 ].rank = = 14 :
return cards
return None
@staticmethod
def fullHouse(ranks):
if len (ranks) > 1 and len (ranks[ 0 ]) > = 3 and len (ranks[ 1 ]) > = 2 :
return ranks[ 0 ] + ranks[ 1 ]
return None
@staticmethod
def twoPair(ranks):
if len (ranks) > 1 and len (ranks[ 0 ]) > = 2 and len (ranks[ 1 ]) > = 2 :
return ranks[ 0 ] + ranks[ 1 ]
return None
@staticmethod
def highCard(ranks):
def sort_key(item):
return item[ 0 ].rank
ranks.sort(key = sort_key, reverse = True )
return ranks[ 0 ][ 0 ].rank
@classmethod
def evaluate( cls , hand):
ranks = hand.ranks()
suits = hand.suits()
high_card = cls .highCard(ranks)
if (cards : = cls .royalFlush(suits)):
return cls .ROYAL_FLUSH, cards, high_card
elif (cards : = cls .straightFlush(suits)):
return cls .STRAIGHT_FLUSH, cards, high_card
elif (cards : = cls .ofAKind(ranks, 4 )):
return cls .FOUR_OF_A_KIND, cards, high_card
elif (cards : = cls .fullHouse(ranks)):
return cls .FULL_HOUSE, cards, high_card
elif (cards : = cls .flush(suits)):
return cls .FLUSH, cards, high_card
elif (cards : = cls .straight(ranks)):
return cls .STRAIGHT, cards, high_card
elif (cards : = cls .ofAKind(ranks, 3 )):
return cls .THREE_OF_A_KIND, cards, high_card
elif (cards : = cls .twoPair(ranks)):
return cls .TWO_PAIR, cards, high_card
elif (cards : = cls .ofAKind(ranks, 2 )):
return cls .ONE_PAIR, cards, high_card
else :
return cls .HIGH_CARD, high_card, high_card
flop = Hand([Card(card) for card in [ "KS" , "QS" , "JS" ]])
print ( "Royal Flush" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "AS" , "10S" ]]) + flop))
print ( "Straight Flush" , Rules.evaluate(Hand([Card(card) for card in [ "KC" , "KD" , "JH" , "10S" , "9S" ]]) + flop))
print ( "Four of a Kind" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "AS" , "10H" ]]) + flop))
print ( "Full House" , Rules.evaluate(Hand([Card(card) for card in [ "KC" , "KD" , "QH" , "5S" , "4H" ]]) + flop))
print ( "Flush" , Rules.evaluate(Hand([Card(card) for card in [ "2S" , "4S" , "2C" , "4C" , "5S" ]]) + flop))
print ( "Straight" , Rules.evaluate(Hand([Card(card) for card in [ "AD" , "2D" , "3H" , "4C" , "5C" ]]) + flop))
print ( "Three of a kind" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "AH" , "5H" , "3D" ]]) + flop))
print ( "Two Pair" , Rules.evaluate(Hand([Card(card) for card in [ "AC" , "AD" , "2S" , "2D" , "3D" ]]) + flop))
print ( "One Pair" , Rules.evaluate(Hand([Card(card) for card in [ "AS" , "AD" , "3H" , "4C" , "5C" ]]) + flop))
|
Is this the right thread?
|