自制弹出输入框测试程序.py

0 Comments

"""
   自制弹出输入框测试程序.py 这是一个研究如何自制弹出输入框的测试程序,
   (由于对turtle的textinput命令不满意)
"""
from tkinter import *
import sys

class popupWindow:
    def __init__(self,master):
        top=self.top=Toplevel(master)
        top.withdraw()
        m_x = master.winfo_rootx()
        m_y = master.winfo_rooty()
        p_width= master.winfo_reqwidth()
        p_height= master.winfo_reqheight()
        s_width= self.top.winfo_reqwidth()
        s_height= self.top.winfo_reqheight()
        
        print('master.winfo_rootx=',m_x)
        print('master.winfo_rooty=',m_y)
        
        print('p_width=',p_width)
        print('p_height=',p_height)
        print('s_width=',s_width)
        print('s_height=',s_height)
        width = p_width//3
        height = 60
        self.top.geometry("%dx%d+%d+%d" % (p_width//3,60,master.winfo_rootx()+(p_width-width)//2,
                                      master.winfo_rooty()+p_height - height*2))
        self.l=Label(top,text="请输入文本:")
        self.l.pack()
        self.e=Entry(top,width=p_width//24)
        self.e.pack()
        self.b=Button(top,text=' 确  定 ', command=self.cleanup)
        self.b.pack()
        self.top.deiconify()  #显示
        
    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()
    c = Canvas(width=800,height=600,bg='yellow')
    c.pack()
    m=mainWindow(root)
    root.mainloop()

发表评论