0
這是我第一次使用Tkinter和Python。我想在循環中運行一系列4幅圖像。用戶通過點擊每個圖像與界面進行交互。我的第四張圖片上有一個複合文字。當程序再次啓動時,我無法使這個複合文本消失。我的複合文本不會刷新我的Tkinter圖像
這是代碼啓動複合文本行:
displayButton.configure(image=photo4, text = sc , compound=CENTER, fg = 'Black', font='Verdana 30 bold', command=callback, wraplength=250, justify=CENTER)
currentpicture=4
這是配置displayButton的代碼,但它不工作。
if currentpicture==4:
displayButton.configure(image=photo1)
currentpicture=1
任何人都可以請幫忙嗎?
這是完整的代碼:
import sys
import cwiid
import time
import math
import numpy
from Tkinter import*
# declares the variable currentpicture to be 1
currentpicture = 1
lt = [None]*100
lb = [None]*100
rt = [None]*100
rb = [None]*100
right_top_cal = 0
left_top_cal = 0
right_bottom_cal = 0
left_bottom_cal = 0
right_top = 0
left_top = 0
right_bottom = 0
left_bottom = 0
# defines the function for the callback button
# declares the screen and currentpicture as global variables
# checks the current picture and change it :)
def callback():
global currentpicture
global roundedresult
global displayButton
global sock
global wiimote
global comment
if currentpicture==4:
displayButton.configure(image=photo1)
currentpicture=1
if currentpicture==3:
count = 0
count2 = 0
# Probably a good idea to have recently calibrated your Balance Board on your Wii
balance_calibration = wiimote.get_balance_cal()
# print balance_calibration
right_top_cal = balance_calibration[0][0]
right_bottom_cal = balance_calibration[1][0]
left_top_cal = balance_calibration[2][0]
left_bottom_cal = balance_calibration[3][0]
print 'Calibration complete'
while (count <= 100):
# Quite a slow refresh rate. Feel free to reduce this time (it's in seconds)
time.sleep(0.1)
wiimote.request_status()
right_top = wiimote.state['balance']['right_top'] - right_top_cal
right_bottom = wiimote.state['balance']['right_bottom'] - right_bottom_cal
left_top = wiimote.state['balance']['left_top'] - left_top_cal
left_bottom = wiimote.state['balance']['left_bottom'] - left_bottom_cal
count2 += 1
if (count2 < 10):
print 'wait'
elif (count2 >= 10):
if (count < 100):
lt[count] = left_top
lb[count] = left_bottom
rt[count] = right_top
rb[count] = right_bottom
count += 1
print left_top
elif (count == 100):
result = (numpy.std(lt, axis=0) + numpy.std(lb, axis=0) + numpy.std(rb, axis=0) + numpy.std(rb, axis=0))
print ('%.2f'%result)
roundedresult = ('%.2f'%result)
score = (1000 - float(roundedresult))
score2 = int(score)/90
score2 = ('%.2f'%score)
if score2 <=3:
comment = 'Thanks for trying, have another go'
elif score2 <=6:
comment = 'Not bad but room for improvement'
elif score2 <=9:
comment = 'Your Balance is great'
elif score2>9:
comment = 'Balance Master'
sc= score2,comment
count += 1
displayButton.configure(image=photo4, text = sc , compound=CENTER, fg = 'Black', font='Verdana 30 bold', command=callback, wraplength=250, justify=CENTER)
currentpicture=4
if currentpicture==2:
# Wii Balance Board
rpt_mode = 0
# Change the number below to the Bluetooth MAC address of your Wii Balance Board
# You get that by pressing red sync button in battery compartment and then running
# hcitool scan
wiimote = cwiid.Wiimote("00:22:4C:42:06:C0")
rpt_mode ^= cwiid.RPT_EXT
wiimote.rpt_mode = rpt_mode
print 'Pairing complete'
displayButton.configure(image=photo3)
currentpicture=3
if currentpicture==1:
roundedresult = None
displayButton.configure(image=photo2, text=roundedresult, compound =CENTER)
currentpicture=2
root = Tk()
photo1=PhotoImage(file='1.gif')
photo2=PhotoImage(file='2.gif')
photo3=PhotoImage(file='3.gif')
photo4=PhotoImage(file='4.gif')
displayButton=Button(None, text=None)
displayButton.pack()
# gives a size to the screen and places it in the center
# gives a title to the screen
root.geometry('320x240+0+0')
root.title('BALANCE BOARD')
## removes decoration from the window
root.overrideredirect(1)
displayButton['image']=photo1
displayButton['command']=callback
displayButton.pack
#puts an image of height 240 into the label and packs it into an appropriate size
root.mainloop()
代碼中的縮進不正確。你能修好嗎? – 2014-09-01 14:31:07
當你說「這是配置displayButton的代碼,但它不起作用。」,「不起作用」是什麼意思?你有錯誤嗎?如果是這樣,有什麼錯誤? – 2014-09-01 14:32:34
親愛的布萊恩,我剛剛修好了縮進。當我說它不起作用時,我的意思是,當我按下按鈕時:第4張圖像變爲第1張圖像,但從屏幕4產生的文本繼續顯示在屏幕上。 – 2014-09-01 14:54:37