创建一个带有 Entry 字段的弹出消息框 [英] Creating a popup message box with an Entry field

查看:33
本文介绍了创建一个带有 Entry 字段的弹出消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个弹出消息框,提示用户输入一个输入.我在一个类中有这个方法.我的代码基于

点击我"窗口:

点击我"打开时的主窗口:

I want to create a popup message box which prompts user to enter an input. I have this method inside a class. I am basing my code on this guide by java2s.

class MyDialog:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)

        Label(top, text="Value").pack()

        self.e = Entry(top)
        self.e.pack(padx=5)

        b = Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):
        print "value is", self.e.get()
        self.top.destroy()

root = Tk()
d = MyDialog(root)

root.wait_window(d.top)

But in this, top = self.top = Toplevel(parent) doesn't work for me.

I have a mockup of what I am trying to accomplish.

My program structure looks something like this:

class MainUI:
   def__int__(self):
       ...
       self.initUI()

   def initUI(self):
       .......
       Popup = Button(self, text="Enter Value", command=self.showPopup)

   def showPopup(self):
       #create the popup with an Entry here

How can I create a message box in Python which accepts user input?

解决方案

I'm a little confused about your two different blocks of code. Just addressing the first block of code, nothing happens because you never enter the mainloop. To do that, you need to call root.mainloop(). The typical way of doing this is to add a button to root widget and bind a callback function to the Button (which includes d=MyDialog() and root.wait_window(d.top))

Here's some basic code which I hope does what you want ...

from Tkinter import *
import sys

class popupWindow(object):
    def __init__(self,master):
        top=self.top=Toplevel(master)
        self.l=Label(top,text="Hello World")
        self.l.pack()
        self.e=Entry(top)
        self.e.pack()
        self.b=Button(top,text='Ok',command=self.cleanup)
        self.b.pack()
    def cleanup(self):
        self.value=self.e.get()
        self.top.destroy()

class mainWindow(object):
    def __init__(self,master):
        self.master=master
        self.b=Button(master,text="click me!",command=self.popup)
        self.b.pack()
        self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
        self.b2.pack()

    def popup(self):
        self.w=popupWindow(self.master)
        self.b["state"] = "disabled" 
        self.master.wait_window(self.w.top)
        self.b["state"] = "normal"

    def entryValue(self):
        return self.w.value


if __name__ == "__main__":
    root=Tk()
    m=mainWindow(root)
    root.mainloop()

I get the value from the popupWindow and use it in the main program (take a look at the lambda function associated with b2).

Main window:

"Click me" window:

Main window while "click me" is open:

这篇关于创建一个带有 Entry 字段的弹出消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆