首页 > 解决方案 > 单击一个按钮并前进到修补程序中窗口的限制

问题描述

我正在尝试从书中学习 python。我正在尝试用修补匠做一个练习。单击一个按钮并前进到窗口的极限。然后总是通过单击相同的按钮将圆圈推进到窗口的另一侧。谢谢你的帮助。

from tkinter import *

def avance(n, g):
    global x, y, step
    # nouvelles coordonnées :
    x[n] = x[n] + g # deplacement de l'axe des x
    # déplacement du dessin dans le canevas :
    can.coords(bille[n], x[n]-10, y[n]-10, x[n]+10, y[n]+10)

    # affichage pour info du x:
    Info.configure(text="Coordonnée x = " + str(x[n]))
    
    i = 0 
             
    if x[n] >= 50:
        while i <= 400:
            step = +20
            i = i+5
            
    if x[n] >= 400:
        step = -20
        i = i-5

    return step          

    


def go():
    avance(0, step)


bille = [0]      # liste servant à mémoriser les références du cercle
x = [50]     # X de départ
y = [100]    # y de départ
step = 0   # "pas" de déplacement initial

# Construction de la fenêtre :
fen = Tk()
fen.title("avance quand on clique jusqu'à la limite de la fenêtre et revient")


Info = Label(fen) # pour afficher l'info du x
Info.grid(row=3, column=0)

# Canvas :
can = Canvas(fen, bg="white", width=400, height=200)
can.grid(row=2, column=0, columnspan=2)
bille[0] = can.create_oval(x[0]-10, y[0]-10, x[0]+10, y[0]+10,
                           fill="blue")

# bouton avance :
f = Frame(fen)
f.grid(row=4, column=0, sticky=W, padx=10)

Button(f, text="Go", fg='blue', command=go).pack(side=LEFT)

fen.mainloop()

问题是当球在开始第二个循环时达到限制(第一个while循环)时,它会返回到第一个的条件。

对不起我的英语不好

感谢帮助

标签: pythontinker

解决方案


我解决了我的问题。这是我的解决方案

from tkinter import *

def avance(n, g):
    global x, y, step
    # nouvelles coordonnées :
    x[n] = x[n] + g # deplacement de l'axe des x
    # déplacement du dessin dans le canevas :
    can.coords(bille[n], x[n]-10, y[n]-10, x[n]+10, y[n]+10)

    # affichage pour info du x:
    Info.configure(text="Coordonnée x = " + str(x[n]))   

    i = 0   
    
    if 5 >= x[n] < 400:  # 5 superieur ou egale à x et x inferieur à 400 , execute la ligne suivante
        step=+20
        return step
    if 5 <= x[n] >= 400: # 5 inferieur ou egale à x et x superieur ou egale à 400 , execute la ligne suivante    
        step = -20
        return step
        # x egale 50 execute le premier if..

def go():
    avance(0, step)


step = 0 # variable pour le pas d'avancement
bille = [0]      # liste servant à mémoriser les références du cercle
x = [5]     # X de départ
y = [100]    # y de départ
  # "pas" de déplacement initial

# Construction de la fenêtre :
fen = Tk()
fen.title("avance quand on clique jusqu'à la limite de la fenêtre et revient")


Info = Label(fen) # pour afficher l'info du x
Info.grid(row=3, column=0)

# Canvas :
can = Canvas(fen, bg="white", width=400, height=200)
can.grid(row=2, column=0, columnspan=2)
bille[0] = can.create_oval(x[0]-10, y[0]-10, x[0]+10, y[0]+10,
                           fill="blue")

# bouton avance :
f = Frame(fen)
f.grid(row=4, column=0, sticky=W, padx=10)

Button(f, text="Go", fg='blue', command=go).pack(side=LEFT)

fen.mainloop()


推荐阅读