简体   繁体   中英

Can't fully update label with concatenated strings in Python Tkinter

This is my first Tkinter program I am writing from scratch. I've done tutorials on Tkinter, but am really still a newbie to it. Thanks for your patience.

I'm creating a 'Binary Helper' for my students in Tkinter, but I'm partially stuck with the part where I am trying to concatenate string values to display in a label. When I click one of the 'bit' buttons, it should automatically update the 'on/off' image, display '0' or '1' in a label, display the bit value in another label. It does all those things correctly, but when it should also display the 'combined' binary value at the top in a different label, it works ok when 'switching' them 'on' one at a time, but when I 'switch' it off AND any other value is 'on' it displays the binary value at the top as all being 'off' ie '00000000' instead of the actual binary value...

FIY: I know I haven't coded the 'denary' value yet...I shall do so after this problem is solved.

Any ideas, advice or help is greatly appreciated.

Here's my code so far...

#Binary Helper with OOP - edujo (11/04/2018)
#Version 6

from tkinter import *
from PIL import Image, ImageTk

tk = Tk()
tk.title("Binary Helper")
tk.configure(bg = "blue")
#Ensure that window can't be resized
tk.resizable(0,0)
#Place the window on top of all other windows
tk.wm_attributes("-topmost", 1)

#Images On/Off
on = "On.png"
off = "Off.png"

class Buttons():
    """Binary Helper"""

    def __init__(self, btnRow=1, btnColumn=1, bitVal="0", bitRow=1, bitColumn=1,
                 binVal="0", binRow=1, binColumn=1, imgFile=off, imgRow=8, imgColumn=1):

        self.btnRow = btnRow
        self.btnColumn = btnColumn
        self.bitVal = bitVal
        self.bitRow = bitRow
        self.bitColumn = bitColumn
        self.binVal = binVal
        self.binRow = binRow
        self.binColumn = binColumn
        self.imgFile = imgFile
        self.imgRow = imgRow
        self.imgColumn = imgColumn

    def switching(self, event):

        #Switching binVal 0/1
        if self.binVal == "1":
            self.binLbl.configure(text = "0")
            self.binVal = "0"
            self.imgFile = Image.open(off)
            self.imgFile = ImageTk.PhotoImage(self.imgFile)
            self.imgLbl = Label(tk,image = self.imgFile)
            self.imgLbl.image = self.imgFile
            self.imgLbl.grid(row = self.imgRow, column = self.imgColumn, pady=10)
        elif self.binVal == "0":
            self.binLbl.configure(text = "1")
            self.binVal = "1"
            self.imgFile = Image.open(on)
            self.imgFile = ImageTk.PhotoImage(self.imgFile)
            self.imgLbl = Label(tk,image = self.imgFile)
            self.imgLbl.image = self.imgFile
            self.imgLbl.grid(row = self.imgRow, column = self.imgColumn, pady=10)

        #Switching bitVal 0/bitVal
        if (self.bitVal == "128" or self.bitVal == "64" or self.bitVal == "32" or
            self.bitVal == "16" or self.bitVal == "8" or self.bitVal == "4" or
            self.bitVal == "2" or self.bitVal == "1"):
            self.bitLbl.configure(text = "0")
            self.bitVal = "0"
        elif self.bitVal == "0":
            if self.bitVal == btn128.bitVal:
                self.bitLbl.configure(text = "128")
                self.bitVal = "128"
            if self.bitVal == btn64.bitVal:
                self.bitLbl.configure(text = "64")
                self.bitVal = "64"
            if self.bitVal == btn32.bitVal:
                self.bitLbl.configure(text = "32")
                self.bitVal = "32"
            if self.bitVal == btn16.bitVal:
                self.bitLbl.configure(text = "16")
                self.bitVal = "16"
            if self.bitVal == btn8.bitVal:
                self.bitLbl.configure(text = "8")
                self.bitVal = "8"
            if self.bitVal == btn4.bitVal:
                self.bitLbl.configure(text = "4")
                self.bitVal = "4"
            if self.bitVal == btn2.bitVal:
                self.bitLbl.configure(text = "2")
                self.bitVal = "2"
            if self.bitVal == btn1.bitVal:
                self.bitLbl.configure(text = "1")
                self.bitVal = "1"

        #Calculating the binary value
        if self.binVal == btn128.binVal and self.binVal == "1":
            a = "1"
        else:
            a = "0"
        if self.binVal == btn64.binVal and self.binVal == "1":
            b = "1"
        else:
            b = "0"
        if self.binVal == btn32.binVal and self.binVal == "1":
            c = "1"
        else:
            c = "0"
        if self.binVal == btn16.binVal and self.binVal == "1":
            d = "1"
        else:
            d = "0"
        if self.binVal == btn8.binVal and self.binVal == "1":
            e = "1"
        else:
            e = "0"
        if self.binVal == btn4.binVal and self.binVal == "1":
            f = "1"
        else:
            f = "0"
        if self.binVal == btn2.binVal and self.binVal == "1":
            g = "1"
        else:
            g = "0"
        if self.binVal == btn1.binVal and self.binVal == "1":
            h = "1"
        else:
            h = "0"
        #Concatenate result
        binTotal = a+b+c+d+e+f+g+h
        binaryAnswerLbl.configure(text = binTotal)

    def createButtons(self):

        #Creating the Bit values
        self.bitLbl = Label(tk, text = self.bitVal, font = "Calibri 16", fg = "yellow", bg = "blue")
        self.bitLbl.grid(row = self.bitRow, column = self.bitColumn)

        #Creating the Binary on/off value
        self.binLbl = Label(tk, text = self.binVal, font = "Calibri 16", fg = "yellow", bg = "blue")
        self.binLbl.grid(row = self.binRow, column = self.binColumn)

        #Creating the On/Off images
        self.imgFile = Image.open(off)
        self.imgFile = ImageTk.PhotoImage(self.imgFile)
        self.imgLbl = Label(tk,image = self.imgFile)
        self.imgLbl.image = self.imgFile
        self.imgLbl.grid(row = self.imgRow, column = self.imgColumn, pady=10)

        #Creating the buttons
        self.btn = Button(tk,text = "On/Off")
        self.btn.bind("<Button-1>",self.switching)        
        self.btn.grid(row = self.btnRow, column = self.btnColumn)

#Creating Main Heading
headingLbl = Label(tk, text = "Binary Helper", width = 30, font = "Calibri 32", fg = "white", bg = "blue")
headingLbl.grid(columnspan = 10, pady = 1)

#Creating Space 1
spaceLbl = Label(tk, bg = "blue")
spaceLbl.grid(columnspan = 10)

#Creating the Binary labels in row 1
binaryLbl = Label(tk, text = "Binary:", font = "Calibri 16", fg = "white", bg = "blue")
binaryLbl.grid(row = 2, column = 1, columnspan = 2)
binaryAnswerLbl = Label(tk, text = "00000000", font = "Calibri 16", fg = "black", bg = "white")
binaryAnswerLbl.grid(row = 2, column = 2, columnspan = 3)

#Creating the Denary labels in row 1
varDenary = StringVar()
denaryLbl = Label(tk, text = "Denary:", font = "Calibri 16", fg = "white", bg = "blue")
denaryLbl.grid(row = 2, column = 6, columnspan = 2)
denaryAnswerLbl = Label(tk, textvariable = varDenary, font = "Calibri 16", fg = "black", bg = "white")
denaryAnswerLbl.grid(row = 2, column = 7, columnspan = 3)
varDenary.set("219")

#Creating Space 2
spaceLbl = Label(tk, bg = "blue")
spaceLbl.grid(columnspan = 10)

#Creating instances of the Button Class Object
btn128 = Buttons(10,1,"0",4,1,"0",6,1,off,8,1)
btn128.createButtons()
btn64 = Buttons(10,2,"0",4,2,"0",6,2,off,8,2)
btn64.createButtons()
btn32 = Buttons(10,3,"0",4,3,"0",6,3,off,8,3)
btn32.createButtons()
btn16 = Buttons(10,4,"0",4,4,"0",6,4,off,8,4)
btn16.createButtons()
btn8 = Buttons(10,5,"0",4,5,"0",6,5,off,8,5)
btn8.createButtons()
btn4 = Buttons(10,6,"0",4,6,"0",6,6,off,8,6)
btn4.createButtons()
btn2 = Buttons(10,7,"0",4,7,"0",6,7,off,8,7)
btn2.createButtons()
btn1 = Buttons(10,8,"0",4,8,"0",6,8,off,8,8)
btn1.createButtons()

#Creating Space 3
spaceLbl = Label(tk, bg = "blue")
spaceLbl.grid(columnspan = 10)

...and here are the 2 images:

off on

In the if-else statements under #Calculating the binary value , you check for self.binVal == "1" on every if. When self.binVal is 0, all conditions become False and ah all become zero. You should check

But you already have all the ones and zeros in binVal , so why don't you concatenate those?

binTotal = btn128.binVal + btn64.binVal + btn32.binVal + btn16.binVal + btn8.binVal + btn4.binVal + btn2.binVal + btn1.binVal
binaryAnswerLbl.configure(text = binTotal)

You can do something similar for denaryLbl :

decTotal = int(btn128.bitVal) + int(btn64.bitVal) + int(btn32.bitVal) + int(btn16.bitVal) + int(btn8.bitVal) + int(btn4.bitVal) + int(btn2.bitVal) + int(btn1.bitVal)
varDenary.set(str(decTotal))  


Also, you're storing a lot of numbers as strings, which doesn't have to be a problem but may not always be the most intuitive (so "1" instead of 1 , "128" instead of 128 ). Moreover, you use a StingVar for a label that should show a number, why not use IntVar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM