I'm attempting to program my own connect four game using Python. I'm trying to sort the circles that I have drawn into a 2d array. However when I try to assign my shape object to the array it gives me an index error. I can't really see an issue with counterrow and countrercolumn, can anyone else? Btw my space class just has an initialiser setting x1, x2, y1, y2, taken, and id
from tkinter import *
from space import *
master = Tk();
w = Canvas(master, width = 600, height = 500)
w.pack()
spaceList = []
for i in range(7):
spaceList.append([0] * 6)
currentmove = 'PLAYER1'
won = False
counterrow = 0
countercolumn = 0
for i in range(0,560,80):
for j in range(0,480,80):
w.create_oval(10+i, 10+j, 90+i, 90+j)
newspace = Space(10+i, 10+j, 90+i, 90+j, False, 'EMPTY')
spaceList[counterrow][countercolumn] = newspace
countercolumn = countercolumn + 1
counterrow = counterrow + 1
while(not won):
movecol = int(input("Please select a column!"))
def move(column):
for i in spaceList:
return 0
mainloop()
You have to reset the countercolumn:
for i in range(0,560,80):
# add this:
countercolumn = 0
for j in range(0,480,80):
# omitted
Otherwise it becomes seven and larger and you get an overflow.
Related
I have some tkinter widgets stored in a list. I'd like to search for an object in that list, by I don't know the syntax.
import tkinter as tk
main = tk.Tk()
base = tk.Frame(main).pack()
l = []
for i in range(3):
et = tk.Label(base, text='label '+str(i))
et.pack()
l.append(et)
print(base.Label.!label in l)
main.mainloop()
Note.
Certainly this is a minimal example to understand where my mistake is.
The gui actually consists of an n x m matrix of tkinker entries, whose cells, rows, and columns should be dynamically added, deleted, modified, and even inserted or switched.
To do this, I have a dictionary that associates index tuples (i,j) with tkinter entries. When an entry is chosen with the mouse, that object is known, but what I really need to know is its index (i,j) to manage all the rest of the information (maths operations over arrays, etc).
To solve your problem you could use the index of separate list and use widget.grid_info() to retrieve the indices.
top_bar = []
sidebar = []
The rendering and the content have separated masters to achieve that the indexes of the lists, which starts with 0, match with the column and row of your inner grid.
The outer master could be populated like this, important to note is that Label on grid position 0,0 is not append to any of the lists but is gridded to fill the space. The grid_configure is just a optical improvement.
def outer_matrix():
for x in range(COLUMNS+1):
for y in range(ROWS+1):
ref = tk.Label(root,)#justify='center')
txt = None
if x == 0 and y != 0:
txt = y
top_bar.append(ref)
elif y == 0 and x != 0:
txt = x
sidebar.append(ref)
if txt != None or x == 0 and y == 0:
ref.config(text = txt, width=10)
ref.grid(row = y, column = x, sticky='nswe')
root.grid_columnconfigure(x,weight=1)
root.grid_rowconfigure(y,weight=1)
Full Example:
import tkinter as tk
ROWS = 6
COLUMNS = 6
top_bar = []
sidebar = []
def outer_matrix():
for x in range(COLUMNS+1):
for y in range(ROWS+1):
ref = tk.Label(root,)#justify='center')
txt = None
if x == 0 and y != 0:
txt = y
top_bar.append(ref)
elif y == 0 and x != 0:
txt = x
sidebar.append(ref)
if txt != None or x == 0 and y == 0:
ref.config(text = txt, width=10)
ref.grid(row = y, column = x, sticky='nswe')
root.grid_columnconfigure(x,weight=1)
root.grid_rowconfigure(y,weight=1)
else:
ref.destroy()
def inner_matrix():
for x in range(COLUMNS):
for y in range(ROWS):
ref = tk.Entry(inner_frame, width=10)
ref.grid(column=x,row=y, sticky='nswe')
inner_frame.grid_columnconfigure(x,weight=1)
inner_frame.grid_rowconfigure(y,weight=1)
ref.bind('<FocusIn>', lambda e:highlight(e))
ref.bind('<FocusOut>', lambda e:highlight(e))
def highlight(event):
info = event.widget.grid_info()
x_info = info['row']
y_info = info['column']
if 'FocusIn' in str(event):
top_bar[x_info].configure(bg='yellow')
sidebar[y_info].configure(bg='yellow')
if 'FocusOut' in str(event):
top_bar[x_info].configure(bg='SystemButtonFace')
sidebar[y_info].configure(bg='SystemButtonFace')
root = tk.Tk()
root.update()
inner_frame = tk.Frame(root,bg='red')
inner_frame.grid(row=1,column=1,
columnspan=COLUMNS,
rowspan = ROWS,
sticky = 'nswe')
outer_matrix()
inner_matrix()
root.mainloop()
I'm trying to figure out why the while loop in one of my functions is still running even after the points in my graphics are equal, which is when I set it to stop. Is there anything I'm doing wrong? I've tried to switch other things around to get it to work but no luck.
It's for a game--when the character reaches the endbox the loop needs to break, but it isn't doing that after I explicitly coded it to. It's in the second function I have:
from graphics import *
def field():
#creating the window
win = GraphWin('The Field',400,400)
win.setBackground('white')
#drawing the grid
boxlist = []
for i in range(0,400,40):
for j in range(0,400,40):
box = Rectangle(Point(i,j),Point(i+40,j+40))
box.setOutline('light gray')
box.draw(win)
boxlist.append(box)
#creating other boxes
startbox = Rectangle(Point(0,0),Point(40,40))
startbox.setFill('lime')
startbox.setOutline('light gray')
startbox.draw(win)
endbox = Rectangle(Point(360,360),Point(400,400))
endbox.setFill('red')
endbox.setOutline('light gray')
endbox.draw(win)
boxlist.append(startbox)
boxlist.append(endbox)
#creating Pete
pete = Rectangle(Point(2,2),Point(38,38))
pete.setFill('gold')
pete.draw(win)
return win,boxlist,pete
def move(win2,boxlist,pete,endbox):
peteloc = pete.getCenter()
#creating loop to move pete
while peteloc != endbox.getCenter():
click = win2.getMouse()
x = click.getX()
y = click.getY()
peteloc = pete.getCenter()
petex = peteloc.getX()
petey = peteloc.getY()
#moving pete
if x>=petex+20 and y<=petey+20 and y>=petey-20:
pete.move(40,0)
elif x<=petex-20 and y<=petey+20 and y>=petey-20:
pete.move(-40,0)
elif y>=petey+20 and x<=petex+20 and x>=petex-20:
pete.move(0,40)
elif y<=petey-20 and x<=petex+20 and x>=petex-20:
pete.move(0,-40)
peteloc = pete.getCenter()
# The main function
def main():
win2,boxlist,pete = field()
endbox = boxlist[len(boxlist)-1]
move(win2,boxlist,pete,endbox)
main()
I think maybe it is caused by precision of float. I guess pete.getCenter() and endbox.getCenter() are something like [float, float], you should avoid using != between float, such as 1.0000001 is not equal to 1.
So even if the character reaches the endbox, the position will still get a little float bias.
So you can change a != b to abs(a - b) > acceptable_error when the error is acceptable. Sample code is like:
# while peteloc != endbox.getCenter():
while abs(peteloc.getX() - endbox.getCenter().getX()) > 0.01 and abs(peteloc.getY() - endbox.getCenter().getY()) > 0.01:
Hope that will help you.
Zelle graphics Point objects don't appear to ever compare as equal:
>>> from graphics import *
>>> a = Point(100, 100)
>>> b = Point(100, 100)
>>> a == b
False
>>>
We have to extract coordinates and do our own comparison. Although #recnac provides a workable solution (+1), I'm going to suggest a more general one. We'll create a distance() method that's valid for any object that inherits from _BBox, which includes Rectangle, Oval, Circle and Line:
def distance(bbox1, bbox2):
c1 = bbox1.getCenter()
c2 = bbox2.getCenter()
return ((c2.getX() - c1.getX()) ** 2 + (c2.getY() - c1.getY()) ** 2) ** 0.5
We can now measure the distance between objects, horizontally, vertically and diagonally. Since your boxes are moving twenty pixels at a time, we can assume that if they are withing 1 pixel of each other, they are in the same location. Your code rewritten to use the distance() method and other tweaks:
from graphics import *
def field(win):
# drawing the grid
boxlist = []
for i in range(0, 400, 40):
for j in range(0, 400, 40):
box = Rectangle(Point(i, j), Point(i + 40, j + 40))
box.setOutline('light gray')
box.draw(win)
boxlist.append(box)
# creating other boxes
startbox = Rectangle(Point(0, 0), Point(40, 40))
startbox.setFill('lime')
startbox.setOutline('light gray')
startbox.draw(win)
boxlist.append(startbox)
endbox = Rectangle(Point(360, 360), Point(400, 400))
endbox.setFill('red')
endbox.setOutline('light gray')
endbox.draw(win)
boxlist.append(endbox)
# creating Pete
pete = Rectangle(Point(2, 2), Point(38, 38))
pete.setFill('gold')
pete.draw(win)
return boxlist, pete
def distance(bbox1, bbox2):
c1 = bbox1.getCenter()
c2 = bbox2.getCenter()
return ((c2.getX() - c1.getX()) ** 2 + (c2.getY() - c1.getY()) ** 2) ** 0.5
def move(win, pete, endbox):
# creating loop to move pete
while distance(pete, endbox) > 1:
click = win.getMouse()
x, y = click.getX(), click.getY()
peteloc = pete.getCenter()
petex, petey = peteloc.getX(), peteloc.getY()
# moving pete
if x >= petex + 20 and petey - 20 <= y <= petey + 20:
pete.move(40, 0)
elif x <= petex - 20 and petey - 20 <= y <= petey + 20:
pete.move(-40, 0)
elif y >= petey + 20 and petex - 20 <= x <= petex + 20:
pete.move(0, 40)
elif y <= petey - 20 and petex - 20 <= x <= petex + 20:
pete.move(0, -40)
# The main function
def main():
# creating the window
win = GraphWin('The Field', 400, 400)
win.setBackground('white')
boxlist, pete = field(win)
endbox = boxlist[-1]
move(win, pete, endbox)
main()
import tkinter as tk
#messagebox is not imported automatically w/ tkinter
from tkinter import messagebox as tkMessageBox
from tkinter import ttk
from random import random as rand
class Square(object):
""" class to use for each square """
def __init__(self):
self.mine_yn = False
self.flag_yn = False
self.prox_num = 0 # number of nearby mines, parse_mines() will fill this in.
self.button = None # ttk.Button instance.
def parse_mines():
"""Look at how many mines are next to a given square,
store in each Square instance that is inside of sqr_dict. """
global sqr_dict
global mine_frame
print('in parse_mines, sqr_dict='+str(sqr_dict))
def try_a_square(sq): #sq = coordinate string(key)
try:
if sqr_dict[sq].mine_yn == True: return 1
if sqr_dict[sq].mine_yn == False: return 0
except KeyError:
print('KeyError for '+sq)
return 0
n = 0
for x in range(5):
for y in range(4):
#check the 8 adjacent squares.
n = n + try_a_square('x'+str(x+1)+'y'+str(y+1))
n = n + try_a_square('x'+str(x+1)+'y'+str(y ))
n = n + try_a_square('x'+str(x+1)+'y'+str(y-1))
n = n + try_a_square('x'+str(x )+'y'+str(y+1))
n = n + try_a_square('x'+str(x )+'y'+str(y-1))
n = n + try_a_square('x'+str(x-1)+'y'+str(y+1))
n = n + try_a_square('x'+str(x-1)+'y'+str(y ))
n = n + try_a_square('x'+str(x-1)+'y'+str(y-1))
if sqr_dict[('x'+str(x)+'y'+str(y))].mine_yn == False:
(sqr_dict[('x'+str(x)+'y'+str(y))]).prox_num = n
print('x'+str(x)+'y'+str(y)+': '+str(n)) #(debug) print n for each sq
#sqr_dict[('x'+str(x)+'y'+str(y))].button.text=(str(n)) #(debug) show n on each button.
n = 0
def create_mine_field():
global mine_frame
global sqr_dict
sqr_dict = {}
mine_frame = tk.Toplevel(root)
mine_frame.grid()
#what to do if user hit 'X' to close window.
mine_frame.protocol("WM_DELETE_WINDOW", mine_frame_close)
# create grid of squares (buttons)
for x in range(5):
for y in range(4):
coord = 'x'+str(x) + 'y'+str(y)
sqr_dict[coord] = Square()
#print('coord='+coord) #debug
#populate with mines
if ( rand()*100 < mines_pct ):
sqr_dict[coord].mine_yn = True
print(str(sqr_dict[coord].mine_yn))
else:
sqr_dict[coord].mine_yn = False
if sqr_dict[coord].mine_yn:
t = '*'
else: t = ' '
# draw boxes
sqr_dict[coord].button = ttk.Button(mine_frame, text=t, width=3 )
sqr_dict[coord].button.grid(column=x, row=y)
# done, next: parse!
print('in create_mines, sqr_dict='+str(sqr_dict))
#parse_mines()
def root_close():
root.destroy()
def mine_frame_close():
root.destroy()
root = tk.Tk()
root.title("MineSweeper")
mines_pct = 20
start_button = ttk.Button(root,text="Start",command=create_mine_field)
start_button.pack()
root.mainloop()
I'm trying to make a simple minesweeper game with tkinter. If I run the above code a simple mine field appears. However if I uncomment the call to parser() then nothing shows up and it seems like it never finds any mines in the sqr_dict dictionary. (parser() will fill in the numbers of adjacent mines for each square)
I don't understand why this function would cause trouble before it is even called. No mine field appears when it's called. Please kindly give me your suggestions. Thanks!
The reason nothing shows up is because you are using both pack and grid on widgets that are children of the root window. Within any given window you must only use one or the other.
I'm new to Blender and new to Python, on my Layer 1 I have a ball named "BallB".
Now I want to make a simple bubbling-animation using Python in Blender but I'm unable to make a keyframe. This should happen on Layer 2.
I tried many and got many errors... all the Snippets I found didn't work and my script allways crashed with Python-Errors like
RuntimeError: Operator bpy.ops.anim.change ... expected an timeline/animation area to be activated
and many more.
Has anybody some hints for me?
I'd like to learn scripted animations in Blender so I'm very thankful for every hint which advances me ;-)
My Code:
import bpy, math, random
d = 4
anz = 100
frameAnz = 10
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 100
for anz in range (0,anz):
ball = bpy.data.objects["ballB"]
tball = ball.copy()
xpos = -1 * (d/2) + random.randint(0,(d-1))
xpos += random.random()
ypos = -1 * (d/2) + random.randint(0,(d-1))
ypos += random.random()
zpos = random.randint(0,(d-1))
zpos += random.random()
bn = str(anz).zfill(5)
bn = "zz_Ball-" + bn
tball.name = bn
tball.location = (xpos, ypos, zpos)
sz = random.uniform(0.015,0.09)
tball.scale = (sz,sz,sz)
#tball.nodes["Emission"].inputs[1].default_value = 200
tball.select = False
scene.objects.link(tball)
#print ("done!")
obj = bpy.context
for actFrame in range(1,frameAnz):
# scene = bpy.context.scene
# scene.keyframe_insert(data_path="gravity", frame = actFrame)
for ob in scene.objects:
ploc = ob.location
#print (ploc)
xpos = ploc[0]
ypos = ploc[1]
zpos = ploc[2]
zpos = zpos + random.random()
ob.location = (xpos, ypos, zpos)
#ypos = ball.location[1]
#zpos = ball.location]2]
#zpos = zpos - random.random()
#ball.location = (xpoy, ypos, zpos)
#obj.keyframe_insert_menu('Location')
#bpy.context.scene.frame_set(0)
#scene = bpy.context.scene
#scene.keyframe_insert(data_path="Location", frame=actFrame)
Actually it looks so:
You are close, you want to use obj.keyframe_insert(), using the index parameter you can keyframe just the one location value.
One issue you will have is that copying the initial object means the new object will use the same animation data, keeping them moving in unison. You can create a new object and use the same mesh data.
An objects layers property is an array of 20 booleans to specify where it is visible, when you add an object to a scene it will be set to be visible on the active layer, so set this after you link it to the scene.
import bpy, random
d = 4
anz = 100
frameAnz = 20
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 100
ball = bpy.data.objects["ballB"]
for anz in range (0,anz):
xpos = -1 * (d/2) + random.randint(0,(d-1))
xpos += random.random()
ypos = -1 * (d/2) + random.randint(0,(d-1))
ypos += random.random()
zpos = random.randint(0,(d-1))
zpos += random.random()
bn = str(anz).zfill(5)
bn = "zz_Ball-" + bn
tball = bpy.data.objects.new(bn, ball.data)
tball.location = (xpos, ypos, zpos)
sz = random.uniform(0.015,0.09)
tball.scale = (sz,sz,sz)
tball.select = False
scene.objects.link(tball)
tball.layers = [False,True] + [False]*18
for actFrame in range(1,frameAnz):
for ob in scene.objects:
ob.location.z += random.random()
ob.keyframe_insert(data_path='location', index=2, frame=actFrame)
I have a label into which I am going to put content of different sizes. I would like to know how high I need to make the label so that I can size the window so it can stay the same for the different content sizes. I have a strategy, but it seems more complicated then it should be.
I want to set a label to a given width and wraplength:
l = Label(root)
l['width'] = 30
l['wraplength'] = 244
l['text'] = "testing this"
Now I want to query the label to find how many lines are used. l['height'] stays at 0, so the best I have been able to come up with is to use l.winfo_height() and convert the height given in pixels to the number of lines used. Nothing in dir(l) seems to give me the information directly, but this strategy is fragile to font changes and other changes.
Any suggestions?
Update: using Brian Oakley's suggestion (which is similar to what I got on usenet) I have the following approximation to a solution (needs polishing, e.g. doesn't take into account that Label breaks at whitespace):
import Tkinter as Tk
import tkFont
import random
import sys
def genstr (j):
rno = random.randint(4,50)
ret_val = str(j) + ":"
for i in range (0, rno):
ret_val += "hello" + str(i)
return ret_val
def gendata (lh):
ret_val = []
for i in range(0,lh):
ret_val.append (genstr (i))
return ret_val
data = gendata (100)
root = Tk.Tk()
font = tkFont.Font(family='times', size=13)
class lines:
def __init__ (self):
self.lastct = 1 # remember where the cutoff was last work from there
def count (self, text, cutoff = 400):
global font
no_lines = 1
start_idx = 0
idx = self.lastct
while True:
if idx > len (text):
idx = len (text)
# shrink from guessed value
while font.measure (text[start_idx:idx - 1]) > cutoff:
if idx <= start_idx:
print "error"
sys.exit ()
else:
idx -= 1
self.lastct = idx - start_idx # adjust since was too big
# increase from guessed value (note: if first shrunk then done)
while (idx < len (text)
and font.measure (text[start_idx:idx]) < cutoff):
idx += 1
self.lastct = idx - start_idx # adjust since was too small
# next line has been determined
print "*" + text[start_idx:idx-1] + "*"
if idx == len(text) and font.measure (text[start_idx:]) < cutoff:
return no_lines
elif idx == len(text):
return no_lines + 1
else:
no_lines += 1
start_idx = idx - 1
idx = start_idx + self.lastct
lin = lines()
for i in range(0,len(data)):
lin.count(data[i], 450)
for i in range(0,min(len(data),10)):
l = Tk.Label(root)
l.pack()
l['text'] = data[i]
print i
no = lin.count (data[i], 450)
print "computed lines", no
l['width'] = 50
l['justify'] = Tk.LEFT
l['anchor'] = 'w'
l['wraplength'] = 450
l['padx']=10
l['pady'] = 5
l['height'] = no
l['font'] = font
if i % 2 == 0:
l['background'] = 'grey80'
else:
l['background'] = 'grey70'
root.mainloop()
You are correct that the height attribute doesn't change. That attribute doesn't tell you the actual height, only the height it is configured to be. The actual height depends on factors such as how much text is in it, the wrap length, the font, and how the widget geometry is managed.
tkinter Font objects have a measure method which lets you determine how tall and wide a string is for a given font. You can get the font for the widget and use that method to determine how much space is required for your string.