我在这里做错了什么?~~Python中的Tkinter

3
我目前正在参加一门编程课程,其中涉及使用Python的Tkinter函数。 我们的作业是创建一个脚本,显示一个“地球”绕“太阳”旋转。 实际上,这只是利用基本几何形状和三角函数(这是一个麻烦的问题),据我所知,我的代码应该能够工作,但不知为何它继续忽略我的if/elif语句。 我感到困惑似乎是由于缺乏经验和理解,但我希望有人能帮助我找出问题出在哪里。 谢谢您的阅读。 ^-^
#import Tkinter package responsible for constructing a
# graphical user interface, GUI

import Tkinter

#from Tkinter, import Canvas Class, which allows us to draw on the GUI
from Tkinter import Canvas, NW, Scale, HORIZONTAL
from math import cos, sin, radians


#create a child object called root. This object is our window. Tk() is 
#the class constructor to instantiate the window
root = Tkinter.Tk()

#call the method title passing in the title of our window
root.title("Earth Orbit")

#create a child object called canvas. This object is our canvas for
#drawing. We instantiate from the Canvas class, calling the constructor and
#passing in the parent of our canvas, and defining the dimenstions
canvas = Canvas(root,width=600,height=600,bg="white")

#call the grid method so that whatever we place on the canvas will be visible
canvas.grid()

color = canvas.create_polygon(0, 0, 0, 600, 600, 600, 600, 0, fill=
"light blue")
season = canvas.create_text(50,100,text="Winter", anchor=NW)
#intial coordinates for our circle
xpos = 275
ypos = 25

Earth = canvas.create_oval(xpos, ypos, xpos+25, ypos+25, fill="blue", outline="cyan")

x = cos(radians(0))
y = sin(radians(0))
Sun = canvas.create_oval(200, 200, 400, 400, fill="orange", outline="yellow")
def changeCirclePosition(event):
    x = 300 + (200*cos(radians(rectSlider.get())))
    y = 300 + (200*-sin(radians(rectSlider.get())))
    canvas.coords(Earth, x, y, x+25, y+25)
rectSlider = Scale(root,from_=0, to=360,orient=HORIZONTAL,label='Change Position',command=changeCirclePosition)    
canvas.create_window(250,500, anchor=NW, window=rectSlider)

if 0<= rectSlider < 90:
        canvas.itemconfigure(color, fill="green")
        canvas.itemconfigure(season,text="Spring")
elif 90<=rectSlider < 180:
        canvas.itemconfigure(color, fill="yellow")
        canvas.itemconfigure(season,text="Summer")
elif 180 <= rectSlider < 270:
        canvas.itemconfigure(color, fill="orange")
        canvas.itemconfigure(season,text="Autumn")
elif 270 <= rectSlider < 360:
        canvas.itemconfigure(color, fill="light blue")
        canvas.itemconfigure(season,text="Winter")
canvas.focus_set()
root.mainloop()

2
问题到底是什么?请注意,没有多少人会阅读所有的代码,请尝试创建一个最小、完整和可验证的示例 - Andrea Corbellini
1个回答

3

你必须在回调函数中放置这些比较。另外,你不能将Scale本身与整数进行比较,你必须获取它的值,就像你对xy所做的那样:

def changeCirclePosition(event):
    x = 300 + (200*cos(radians(rectSlider.get())))
    y = 300 + (200*-sin(radians(rectSlider.get())))
    canvas.coords(Earth, x, y, x+25, y+25)
    if 0<= rectSlider.get() < 90:
            canvas.itemconfigure(color, fill="green")
            canvas.itemconfigure(season,text="Spring")
    elif ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接