0

I'm trying to learn the best way to arrange/define code. Taking the below as an example, I have to write the tkMessageBox command twice. I did try to create a def within getText() and refer to that, but it didn't work.

Questions therefore please

1) How could I arrange the code such that I can place the tkMessageBox command in a def or something and refer to it even within getText()

2) Considering best practice, should this code be layed out differently? If so how/why?

Thank you in advance

import Tkinter as tk
import tkMessageBox
import base64
myText = 'empty'
def getText():
 global myText
 myText = inputBox.get()
 entered = "You entered: " + myText
 encoded = "We encoded: " + base64.encodestring(myText)
 Button1 = tk.Button(root, command = tkMessageBox.showinfo("Press me", entered))
 Button1.pack()
 Button2 = tk.Button(root, command = tkMessageBox.showinfo("Press me", encoded))
 Button2.pack()
 root.destroy()
root = tk.Tk()
# Text label
simpleTitle = tk.Label(root)
simpleTitle['text'] = 'Please enter your input here'
simpleTitle.pack()
# The entry box widget
inputBox = tk.Entry(root)
inputBox.pack()
# The button widget
button = tk.Button(root, text='Submit', command=getText)
button.pack()
tk.mainloop()
asked Jul 17, 2012 at 15:46

4 Answers 4

2

I am not sure if you are asking about refactoring your code or not, but the ultimate style guide for laying out/formatting Python code is PEP 8 -- Style Guide for Python Code.

answered Jul 17, 2012 at 15:48
Sign up to request clarification or add additional context in comments.

Comments

1

It's also worth noting that there is a pep8 command line utility that will scan your source code for (most) pep8 violations

pip install pep8
pep8 source_code_to_check
answered Jul 17, 2012 at 16:15

Comments

0

Can't you just assign the tkMessageBox command to a variable, and reference this variable twice? The variable could be placed in your function.

answered Jul 17, 2012 at 15:51

1 Comment

Agreed In this case a variable works, but what if I had 2 or more lines of code and I didn't want to have to keep repeating it. Can I have a def in a def? Thanks
0

It would be good practice to remove any duplicate code such as this, this is a step in the software design process known as refactoring which is mentioned in another answer.

In your particular case for the tkMessageBox example you would do something like this.

command = tkMessageBox.showinfo("Press me", entered)
Button1 = tk.Button(root, command)
Button1.pack()
Button2 = tk.Button(root, command)
Button2.pack()

This will consolidate your command into a single location so that it can be easily maintained later. And while this is most likely not an issue for your code sample, it is a good habit to get into because it will reduce the number of calls necessary and thus optimize your code in the future.

answered Jul 17, 2012 at 15:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.