我的复合文本不会刷新我的Tkinter imag

2024-06-01 08:05:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次使用Tkinter和Python。我想在一个循环中运行一系列的4个图像。用户通过单击每个图像与界面进行交互。我的第四个图像上出现了一个复合文本。当程序再次启动时,我无法使此复合文本消失。你知道吗

这是启动复合文本的代码行:

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()

Tags: theimagerightnoneiftopcountleft