1

im trying to build some code that will open new Tkinter window and then there will be a text box that the client would enter text there, and the text will be saved in some variable in my code. i dont understand what doesn`t work, the window is not being opened. Help please..

import sys
from Tkinter import *

def mhello():
    mtext = ment.get()
    mlabel2 = Label(mGui, text = mtext).pack()
    return

mGui = Tk()
ment = StringVar()

mGui.geometry('450x450+500+300')
mGui.title('Nir`s ScreenShare')

mlabel = Label(mGui,text='My Label').pack()

mbutton = Button(mGui, text = 'OK', command = mhello,fg = 'red', bg='blue').pack()

mEntry = Entry(mGui,textvariable=ment).pack()

** im working with python 2.7 if it matters

1
  • 1
    Hello. Looking at your question history, you haven't accepted an answer yet. If any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. Of course, there is no obligation to do this. Commented Jun 20, 2017 at 8:17

4 Answers 4

7

For every tkinter code, you will need a mainloop(), add this at the end of your script:

mGui.mainloop()

And also, as a hint:

mEntry, mbutton, mlabel are all going to be None because .pack() doesn't return anything. You will need to do them at separate lines.

For example:

mlabel = Label(mGui,text='My Label')
mlabel.pack()
Sign up to request clarification or add additional context in comments.

4 Comments

hehe, you were 40 seconds faster ;)
Traceback (most recent call last): File "C:/Users/abc/project/textbox.py", line 21, in <module> ment.mainloop() AttributeError: StringVar instance has no attribute 'mainloop'
Are you sure you copied correctly?, there shouldn't be any error
@utannuta I think you copied ment.mainloop() instead of mGui.mainloop()
0

You are missing

mGui.mainloop()

Comments

-1

you need a mainloop line

mGui.mainloop()

1 Comment

Before answering an old question having other answers ensure your answer adds something new or is otherwise helpful in relation to them.There are already two older answers, stackoverflow.com/a/43442884/5698098 and stackoverflow.com/a/43442879/5698098, which your answer duplicates here. Duplication is rarely something good. Please edit your post; you can also refer to this contribution guide.
-1

you need to add

mGui.mainloop()

2 Comments

edit your answer - add code brackets (`/``), correct typo, add maybe more details.
Welcome to Stack Overflow. Before answering an old question having other answers ensure your answer adds something new or is otherwise helpful in relation to them. There're already these two answers (stackoverflow.com/a/43442884/5698098, stackoverflow.com/a/53272877/5698098) which state exactly the same answer you added again today. There's rarely a good reason for duplication. – Please take the tour to learn how Stack Overflow works and also read How do I write a good answer?.

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.