2016-01-25 177 views
3

我現在註冊了一個編碼類,其中的一部分涉及使用Python的Tkinter函數。我們的任務是創建一個腳本,顯示旋轉「太陽」的「地球」。真的,所有這一切都是利用基本的幾何形狀和三角(這是一個工作的屁股疼痛),並且據我所知,我的代碼應該正在工作,但無論出於何種原因,它將繼續忽略我的if/elif語句。我感覺我的困惑是由於缺乏經驗和理解,但我希望有人能幫我弄清楚我做錯了什麼。謝謝你的閱讀。^-^我在這裏做錯了什麼?~~ Python中的Tkinter

#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

究竟是什麼問題?請注意,沒有多少人會閱讀所有代碼,嘗試創建[最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

回答

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 ...