Это мой первый опыт использования 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()
Отказ в коде неправильный. Вы можете исправить это, пожалуйста? –
Когда вы говорите: «Это код, который настраивает displayButton, но он не работает». Что означает «не работает»? Вы получили сообщение об ошибке? Если да, то какая ошибка? –
Дорогой Брайан, я только что исправил отступы. Когда я говорю, что это не работает, я имею в виду это, когда я нажимаю кнопку: четвертое изображение изменяется на 1-ое изображение, но текст, сгенерированный с экрана 4, продолжает отображаться на экране. –