Show the result of every count - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Show the result of every count (/thread-35908.html) Pages:
1
2
|
Show the result of every count - George87 - Dec-28-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" 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() RE: Show the result of every count - menator01 - Dec-28-2021 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. RE: Show the result of every count - George87 - Dec-28-2021 I want the label to be updated by each click but i don't know how to do it... RE: Show the result of every count - menator01 - Dec-28-2021 One way #! /usr/bin/env python3 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() RE: Show the result of every count - menator01 - Dec-28-2021 Another way #! /usr/bin/env python3 # Do the imports import tkinter as tk import sys # Function for increasing or decreasing the counter # action is an argument that is passed to perform the desired action def doit(action): # doit counter is a static variable to hold the current value doit.counter = getattr(doit, 'counter', 0) # if the action argument equals add, increase the counter # elif the action is equal to sub, decrease the counter value # else reset the counter value to 0 if action == 'add': doit.counter += 1 elif action == 'sub': doit.counter -= 1 # If the counter goes less than 0, set counter to 0 if doit.counter <= 0: doit.counter = 0 else: doit.counter = 0 # Update the text in the label 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') # The lambda is a way to pass arguments to the function call 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() RE: Show the result of every count - George87 - Dec-28-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! RE: Show the result of every count - deanhystad - Dec-28-2021 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. import sys import tkinter as tk increment = 0 end_count = 10 def startCounter(start, end, incr): """I configure the counter""" global increment, end_count counter.set(start) increment = incr end_count = end def update(): """I update the count label""" 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) # Run myself again 1 second from now. 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() # Begin the update "loop" root.mainloop() RE: Show the result of every count - menator01 - Dec-28-2021 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 RE: Show the result of every count - deanhystad - Dec-28-2021 I added in some poker rules to identify different hands. Still having a problem with two pair. import collections import copy import random class Card(): """A playing card that has a rank and suit""" # These are class variables. They are associated with the class, not instances of the class 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): # These are instance variables. Each instance has their own rank an suit if suit is None: # Assume rank is a card repr. Extract rank and suit 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): """For sorting and comparing by rank""" return self.rank < other.rank # self.rank is my rank. other.rank is the rank of another card def __gt__(self, other): """For sorting and comparing by rank""" return self.rank > other.rank def __eq__(self, other): """For sorting and comparing by rank""" return self.rank == other.rank def name(self): """Get long name for card""" return f"{self.rank_names[self.rank]} {self.suit}" def __repr__(self): """Get short name for card""" return f"{self.short_rank_names[self.rank]}{self.suit[0]}" class Hand(): """A list of cards""" def __init__(self, cards=None): self.cards = [] if cards is None else cards def ranks(self): """Group cards into ranks. Sort ranks by number of cards in a rank""" def sort_key(item): """Sorting primarily on number of matching cards and secondarily on rank value""" 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): """Group cards into suits. Sort suits by number of cards in a suit""" 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): """Create a new hand by combining two existing hands""" return Hand(self.cards + other.cards) def __repr__(self): """Print cards in hand""" return ", ".join([str(card) for card in self.cards]) class Deck(): """Deck of cards""" 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 number of cards in deck""" return len(self.cards) def deal(self, count): """Deal count cards from top of deck""" 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): """Return cards if we have four card with the same rank""" for rank in ranks: if len(rank) == count: return rank return None @staticmethod def flush(suits): """Return cards if there are 5 of the same suit""" return suits[0] if len(suits[0]) > 4 else None @staticmethod def straight(ranks): """Return cards if there are 5 cards in a row""" def sort_key(item): """Sort ranks by rank value""" return item[0].rank straight = None # Sort the ranks by decreasing value ranks.sort(key=sort_key, reverse=True) if ranks[0][0].rank == 14: # If we have aces, treat them as both rank 14 and rank 1 ranks.append([Card(1, card.suit) for card in ranks[0]]) # look for 5 consecutive ranks 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): """Return cards if there are 5 cards in a row with the same rank""" if (cards := Rules.flush(suits)) is not None: return Rules.straight(Hand(cards).ranks()) return None @staticmethod def royalFlush(suits): """Return cards if there is an ace high straight flush""" if (cards := Rules.straightFlush(suits)) is not None and cards[0][0].rank == 14: return cards return None @staticmethod def fullHouse(ranks): """Return cards if there is a full house""" 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): """Return cards if there are two pairs""" 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): """Return the high card""" def sort_key(item): """Sort ranks by rank value""" 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 # Test different hands 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)) RE: Show the result of every count - menator01 - Dec-28-2021 (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. Is this the right thread? |