用类写tkinter GUI

import tkinter as tk

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('GIS PROCESS TOOL')
        #add menu
        self.menubar=tk.Menu(self)
        self.submenu=tk.Menu(self.menubar)
        self.submenu.add_command(label='load shapefile',command=self.load)
        self.submenu.add_command(label='bbox clip',command=self.clip1)
        self.submenu.add_command(label='polygon clip',command=self.clip2)
        self.menubar.add_cascade(label='operation',menu=self.submenu)
        self.config(menu=self.menubar)
        #add text
        self.str=tk.StringVar()
        self.str.set("hello world\nI am come from china\nnice to meet you")
        self.txt=tk.Message(self,textvariable=self.str)
        self.txt.pack()

        self.btn=tk.Button(self,text="press me please",command=self.print)
        self.btn.pack()

        self.text=tk.Text()
        self.text.pack()
    def print(self):
        print("pressed")
    def load(self):
        print("load called")
        sw=SubWindow(self)
        self.wait_window(sw)
        return
    def clip1(self):
        print("clip1 called")
    def clip2(self):
        print("clip2 called")

class SubWindow(tk.Toplevel):
    def __init__(self,parent):
        super().__init__()
        self.title('subwindow')
        self.parent=parent
        self.txt=tk.Text(self)
        self.txt.place(relx=0.1,rely=0.1,relwidth=0.5,relheight=0.5)



        self.btn=tk.Button(self,text="press me",command=self.print)
        self.btn.place(relx=0.1,rely=0.7)
        self.btn1=tk.Button(self,text="subsub",command=self.showsub)
        self.btn1.place(relx=0.1,rely=0.9)

    def print(self):
        print("pressed")
    def showsub(self):
        ssw=Subsubwindow(self)
        self.wait_window(ssw)
        return

class Subsubwindow(tk.Toplevel):
    def __init__(self,parent):
        super().__init__()
        self.parent=parent
        self.title("subsubwindow")
        self.btn=tk.Button(self,text="press me",command=self.print)
        self.btn.pack()

    def print(self):
        print("pressed")
        print(self.parent.parent.str)




root=MainWindow()
root.mainloop()

用类写tkinter GUI_第1张图片

你可能感兴趣的:(用类写tkinter GUI)