1
from Tkinter import *

root = Tk()
root.title("Whois Tool")

text = Text()
text1 = Text()

text1.config(width=15, height=1)
text1.pack()

def button1():
    text.insert(INSERT, text1.get("1.0", END))

b = Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=60, height=15)
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

root.mainloop()

The above script works without any exception errors, but if I modify import style: import Tkinter as Tk, it will complain about about argument 'LEFT, RIGHT, Y' and I had to make them lower letter in string form as below script, why is that?

import Tkinter as tk

root = tk.Tk()
root.title("Whois Tool")

text = tk.Text()
text1 = tk.Text()

text1.config(width=15, height=1)
text1.pack()

def button1():
    text.insert('insert', text1.get("1.0", 'end'))
#     text.insert(END, text1)

b = tk.Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()

scrollbar = tk.Scrollbar(root)
scrollbar.pack(side='right', fill='y')
text.config(width=60, height=15)
text.pack(side='left', fill='y')
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

root.mainloop()
1
  • The code works for me with python 2.7.8... What Python version do you use? Can you provide a Python traceback or error message? Commented Aug 22, 2014 at 1:51

2 Answers 2

2

LEFT, RIGHT and a few others are constants defined by Tkinter. The values of those constants are the strings "left", "right", etc

When you do from Tkinter import *, those constants get imported along with everything else. When you do import Tkinter as tk, they do not, just as nothing else gets imported. In this case you can refer to them by fully qualifying them with the module name, eg: tk.LEFT, tk.RIGHT, etc.

No matter how you import them, the lowercase strings will always work. Personally I see no use for the constants, since they don't really provide any benefit.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, since both of you provide great answer to my question, I'll choose the newbie's as best answer.
0

When you use "import Tkinter as Tk", you have to call all methods and properties of Tkinter using the object Tk. So instead of "left", "right" you have to use Tk.LEFT, Tk.RIGHT ...

In your first example you used "scrollbar = Scrollbar(root)" But in the second you wrote "scrollbar = tk.Scrollbar(root)"

You have call everything inside Tkinter using the object Tk in your second example. For more information about Python modules you may visit here: http://www.tutorialspoint.com/python/python_modules.htm

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.