简体   繁体   English

Python - Tkinter 重复 function 不工作?

[英]Python - Tkinter repeat function not working?

I am trying to make a function that makes a circle in a random place, in return the first time it executes correctly but after that every time i try to execute it it doesn't work.我正在尝试制作一个 function 在随机位置制作一个圆圈,作为回报,它第一次正确执行但之后每次我尝试执行它都不起作用。 Please help me and here's the code:请帮助我,这是代码:

size = 200

class Spots():
    def __init__(self):
        self.avaible = True
    def taken(self):
        self.avaible = False

a1 = Spots()
a2 = Spots()
a3 = Spots()
b1 = Spots()
b2 = Spots()
b3 = Spots()
c1 = Spots()
c2 = Spots()
c3 = Spots()

spots = (a1,a2,a3,b1,b2,b3,c1,c2,c3)

import random as r
from tkinter import *
tk = Tk()
cv = Canvas(tk, width = 200 + size * 3, height = 200 + size * 3)
cv.pack()

def tkO():
    rch = r.choice(spots)
    x = 100
    y = 100
    
    while a1.avaible == True or a2.avaible == True or a3.avaible == True or b1.avaible == True or b2.avaible == True or b3.avaible == True or c1.avaible == True or c2.avaible == True or c3.avaible == True:
        while rch.avaible == False:
            rch = r.choice(spots)
        break
    rch.taken
    if "a" in str(rch):
        y = 100
        if "1" in str(rch):
            x = 100
        if "2" in str(rch):
            x = 100 + size
        if "3" in str(rch):
            x = 100 + size * 2
    if "b" in str(rch):
        y = 100 + size
        if "1" in str(rch):
            x = 100
        if "2" in str(rch):
            x = 100 + size
        if "3" in str(rch):
            x = 100 + size * 2
    if "c" in str(rch):
        y = 100 + size * 2
        if "1" in str(rch):
            x = 100
        if "2" in str(rch):
            x = 100 + size
        if "3" in str(rch):
            x = 100 + size * 2

    cv.create_oval(x,y,x+size,y+size)

Any help will be appreciated.任何帮助将不胜感激。 Also, if you can find a way to make the mess in the tkO() function more efficient (shorter) that will help too.此外,如果您能找到一种方法使tkO() function 中的混乱变得更高效(更短),那也将有所帮助。

The function is fixed, All I had to do was add a few more variables. function 是固定的,我所要做的就是添加更多变量。 making the if statements with `if "(letter)" in rch being strings and the if statements with class objects - objects (rchobject).使用 rch 中的 `if "(letter)" 作为字符串的 if 语句和使用 class 对象 - 对象 (rchobject) 的 if 语句。

Thanks to Bryan Oakley for helping me figure this out!感谢 Bryan Oakley 帮我解决了这个问题!

import random as r

spots = (a1,a2,a3,b1,b2,b3,c1,c2,c3)
spotsStr = spots but with strings

def tkO():
 rn = r.randint(1,10)
 rch = spotsStr[rn]
 rchobject = spots[rn]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM