2

Easygui inserts curly brackets when we try to break the text is there a way to break the text without it giving the curly brackets.

import easygui

#Class for seperating workers.
class HumanClassification:
    #Sets the default worker info to null:
    def __init__(self):
        self.age = 0
        self.pay = 0
        self.gender = ''

    #Outputs the workers info:
    def classification(self, age, pay, gender):
        self.age = age
        self.pay = pay
        self.gender = gender



#Current Workers:
myListWorkers = ['Bob', 'Diann', 'Tec']
Bob = HumanClassification()
Diann = HumanClassification()
Tec = HumanClassification()



#Instantize Classes:
Bob.classification(42, 15000, 'male')
Diann.classification(25, 1000, 'female')
Tec.classification(18, 200000, 'male')

#Asks user if he/she wants to find info about worker:
bossInput = easygui.buttonbox("Who do you want to view info on? ", choices=myListWorkers)
bossInputNew = eval(bossInput)

output = 'Age:', bossInputNew.age, 'years','old \n',  'Pay:', bossInputNew.pay, 'dollars', 'Gender:', bossInputNew.gender
#Prints out the output from code:
easygui.msgbox(msg=(output))
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
a9ent456
  • 21
  • 2

1 Answers1

1

How to fix this?

easygui.msgbox assumes its msg to be a string, like 'Hello', but you are passing it a tuple, a sequence of objects like 'age', 32, 'salary', 30000.

You can fix the problem by passing a string to easygui.msgbox. Your output is a string with embedded variable values, so this is good case for using a format string. A format string is a string that contains placeholders - usually curly bracket pairs {} - that you can replace with variable values.

Change this line:

output = 'Age:', bossInputNew.age, 'years','old \n',  'Pay:', bossInputNew.pay, 'dollars', 'Gender:', bossInputNew.gender

to:

output = 'Age: {} years old \nPay: {} dollars Gender:{}'.format(bossInputNew.age, bossInputNew.pay, bossInputNew.gender)

And it should work.

Why is this happening?

easygui.msgbox assumes its msg to be a string, but doesn't actually check that this is true before passing msg to the code that creates the GUI elements. As it happens, the default GUI provider is Python's tkinter package, and tkinter ultimately relies on code written in another language, tcl, to render GUI elements on screen.

tcl generally treats all variables as strings. Given a tuple like

'Age: ', 32, 'years old \nPay:', 30000

tcl needs to be able to recognise that the three strings that make up 'years old \nPay:' belong together. The way to do this in tcl is to wrap (or "quote") the string with curly brackets. This is why you see the curly brackets in your message box when you pass it the output tuple.

Community
  • 1
  • 1
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153