473,785 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter help, please...

I've been away from Python for some time, and I'm just starting to look
at Tkinter. Just so you know, I'm coming from Visual Basic, where this
would, I *think*, not have been a problem, so it must be a case of
getting my head around the Tkinter way of doing things.

In a nutshell, I've written an app wherein I wish to update the display
often. What's happening is that I get *no* display until main() hits
win.mainloop(). I've indicated the things I've tried (round about line
180). At the risk of exposing myself, here's the whole thing.

If you have a thought, I'd sure appreciate it!

Nick.

from random import *
from Tkinter import *

'''
Score the "good jack"
'''
def jack(h, f):
hand = h[:]
hand.remove(f)
for card in hand:
if card.rank == 10 and card.suit == f.suit:
return 1
return 0

'''
Count and return 2 for each combination that adds up to 15
'''
def fifteen(h):
if h[0].value() + h[1].value() + h[2].value() + h[3].value() +
h[4].value() == 15:
return 2
# 4-card combos
score = 0
for a in range(0,2):
for b in range(a+1,3):
for c in range(b+1,4):
for d in range(c+1,5):
if h[a].value() + h[b].value() + h[c].value() +
h[d].value() == 15:
score += 2

# 3-card combos
for a in range(0,3):
for b in range(a+1,4):
for c in range(b+1,5):
if h[a].value() + h[b].value() + h[c].value() == 15:
score += 2

# 2-card combos
for a in range(0,4):
for b in range(a+1,5):
if h[a].value() + h[b].value() == 15:
score += 2
return score

'''
Simplified flush rules:
Try the 4 flush and 5 flush, give the best score.
'''
def flushes(h, f):
# 5-flush
suit = h[0].suit
total = 0
for card in h:
if card.suit == suit:
total += 1
#end if
#end for
if total == 5: return 5

# 4-flush
hand = h[:]
hand.remove(f)
suit = hand[0].suit
total = 0
for card in hand:
if card.suit == suit:
total += 1
if total == 4:
return 4
return 0

'''
1 Point per card per run
'''
def runs(h):
# Is there a 5-run?
if h[0].rank == h[1].rank-1 and h[1].rank == h[2].rank-1 and \
h[2].rank == h[3].rank-1 and h[3].rank == h[4].rank-1 :
return 5
# Look for 4-runs:
score = 0
for a in range(0,2):
for b in range(a+1,3):
for c in range(b+1,4):
for d in range(c+1,5):
if h[a].rank == h[b].rank-1 and h[b].rank ==
h[c].rank-1 \
and h[c].rank == h[d].rank-1:
score += 4
if score != 0:
return score

#Look for 3-runs
for a in range(0,3):
for b in range(a+1,4):
for c in range(b+1,5):
if h[a].rank == h[b].rank-1 and h[b].rank == h[c].rank-1:
score += 3
return score

'''
Two points per pair
'''
def pairs(h):

'''
Tally me hand
'''
score = 0
for left in range(0,4):
for right in range(left+1, 5):
if h[left].rank == h[right].rank:
score += 2
return score

def tally(h, f):

return pairs(h) + runs(h) + flushes(h, f) + fifteen(h) + jack(h, f)
class Card:
def __init__(self, r, s):
self.rank = r
self.suit = s

def value(self):
return min([self.rank+1, 10])

def __repr__(self):
s = "HSCD"[self.suit]
r = "A23456789T JQK"[self.rank]
return r+s

__str__ = __repr__

class Deck:
def __init__(self):
self.deck = []
for r in range(13):
for s in range(4):
self.deck.appen d(Card(r,s))
shuffle(self.de ck)
#self.card = 0

def deal(self):
#self.card += 1
if len(self.deck) == 2:
self.deck = []
for r in range(13):
for s in range(4):
self.deck.appen d(Card(r,s))
shuffle(self.de ck)
return self.deck.pop()

class Cribbage:
def __init__(self, win):

#Draw the interface
self.f = Frame(win)
self.f.grid()
self.cardpix = [Label(self.f), Label(self.f), Label(self.f),
Label(self.f), Label(self.f)]
clr = ["red", "blue"]
n = 1
for c in self.cardpix:
c.configure(wid th=10, height=10, bg=clr[n%2], text="card
"+str(n))
c.grid(row=0, column=n-1)
n += 1
self.scorebox = Label(self.f)
self.scorebox.c onfigure(height =5, bg="green", text="Score: 0")
self.scorebox.g rid(row=1, column=0, columnspan=5)

def play(self, win):
d = Deck()
hand = [d.deal(), d.deal(), d.deal(), d.deal()]
flipped = d.deal()
hand.append(fli pped)
hand.sort()
score = tally(hand, flipped)
self.scorebox.c onfigure(text= "Score: " + str(score))
#Eventually, display the actual card images, but for now...
for c, x in zip(hand, self.cardpix):
x.configure(tex t = str(c))
#I've tried both of these, to no avail.
win.iconify()
self.f.update_i dletasks()
return score

def main():
win = Tk()
run = Cribbage(win)
score = 0
best = 0
while score < 24:
score = run.play(win)
if score >= best: best = score
win.mainloop()

main()

--
Posted via a free Usenet account from http://www.teranews.com

May 24 '07 #1
2 1553
I've made a couple of minor changes to your code from the Cribbage class
down:

class Cribbage:
def __init__(self, win):

self.parent = win # <---- make the toplevel Tk window an
# <---- attribute of the class
#Draw the interface
self.f = Frame(self.pare nt)
self.f.grid()
self.cardpix = [Label(self.f), Label(self.f), Label(self.f),
Label(self.f), Label(self.f)]
clr = ["red", "blue"]
n = 1
for c in self.cardpix:
c.configure(wid th=10, height=10, bg=clr[n%2], text="card
"+str(n))
c.grid(row=0, column=n-1)
n += 1
self.scorebox = Label(self.f)
self.scorebox.c onfigure(height =5, bg="green", text="Score: 0")
self.scorebox.g rid(row=1, column=0, columnspan=5)

def play(self):
d = Deck()
hand = [d.deal(), d.deal(), d.deal(), d.deal()]
flipped = d.deal()
hand.append(fli pped)
hand.sort()
score = tally(hand, flipped)
self.scorebox.c onfigure(text= "Score: " + str(score))
#Eventually, display the actual card images, but for now...
for c, x in zip(hand, self.cardpix):
x.configure(tex t = str(c))
#I've tried both of these, to no avail.
self.parent.upd ate() <---- update the toplevel window
return score

def main():
win = Tk()
run = Cribbage(win)
score = 0
best = 0
while score < 24:
score = run.play()
if score >= best: best = score
time.sleep(1) <--- short sleep to see what's happening
win.mainloop()

main()

Regards,

John
May 24 '07 #2
John McMonagle wrote:
I've made a couple of minor changes to your code from the Cribbage class
down:

class Cribbage:
def __init__(self, win):
....
score = run.play()
if score >= best: best = score
time.sleep(1) <--- short sleep to see what's happening
win.mainloop()

main()

Regards,

John
Thank you, Mr. McMonagle!

--
Posted via a free Usenet account from http://www.teranews.com

May 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
12034
by: Dustan | last post by:
I'm trying to get a scrollbar bound with a Frame, and I keep on getting a scrollbar, but it doesn't actually scroll. Some help, please?
1
5255
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call last): File "C:\Documents and Settings\yvesd\Bureau\protowin.py", line 36, in ? b1 = Tkinter.Button (win1)
6
1885
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance button = Tkinter.Button(frame,text = returnstring, command=callback(returnstring))# this line executes on creation my output on startup is (should output when I choose an option)
1
2363
by: vijayca | last post by:
my python installation is:Active python 2.5.1 i am using Red Hat Linux i have the Tkinter module installed but any simple script produces an error.... script: from Tkinter import Label widget = Label(None, text='Hello GUI world!') widget.pack() widget.mainloop()
1
2441
by: alivip | last post by:
I integrat program to be GUI using Tkinter I try browser direction as you can see # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, # ctrl+v to paste, and ctrl+/ to select all # count words in a text and show the first ten items
2
2058
by: Doran, Harold | last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep finding. I like how this document is organized and also how it provides the code with visuals of what should appear on the screen. If there are other docs I should read, please let me know. Second, I am trying to work through a couple of the...
7
3282
by: Protected | last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try some Tkinter examples, including the one from the library reference, but they don't seem to do anything! Shouldn't there be, like, a dialog? I'm running Windows XP and using IDLE. You can assume my version of Python is the latest.
2
2178
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using Active State Python version 2.5 and found that there is not Tkinter and Tk module in it. To use Tkinter do I actually require Tk installed on my machine? Please suggest and where can I find both these modules?
0
1659
by: lee.walczak | last post by:
Hi, I hope someone can help here. I have made lots of progress implementing a gui application which at present uses the TableList python wrapper to interface with Tcl TableList implemention. http://tkinter.unpythonic.net/wiki/TableListTileWrapper, http://tkinter.unpythonic.net/wiki/TableListWrapper I wish to change the default Cell Entry Widget ( for Editing ) to a
3
3918
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.