简体   繁体   English

为什么我的代码不打开 tkinter 窗口?

[英]Why my code doesn`t open tkinter window?

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.我试图构建一些代码来打开新的 Tkinter 窗口,然后会有一个文本框,客户端将在那里输入文本,文本将保存在我的代码中的某个变量中。 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 ** 如果重要的话,我正在使用 python 2.7

For every tkinter code, you will need a mainloop() , add this at the end of your script:对于每个mainloop()代码,您都需要一个mainloop() ,在脚本末尾添加:

mGui.mainloop()

And also, as a hint:而且,作为提示:

mEntry , mbutton , mlabel are all going to be None because .pack() doesn't return anything. mEntrymbuttonmlabel都将是 None 因为.pack()不返回任何东西。 You will need to do them at separate lines.您需要在不同的行中执行它们。

For example:例如:

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

你不见了

mGui.mainloop()

you need a mainloop line你需要一条主循环线

mGui.mainloop() mGui.mainloop()

你需要添加

mGui.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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