Python GUI Programming (Tkinter)

7980 ワード

Tkinterプログラミングの例
#!/usr/bin/python
#coding:utf-8

from Tkinter import *
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

class GUI_WINDOWS:
    def __init__(self, root):
        self.root = Frame(root)
        self.driver = None
        self.friendEdit = None
        self.bStopQuery = False
        self.loadFileName = ''
        self.var = StringVar()
        self.var1 = StringVar()
        self.RbtnVar = IntVar()
        self.RbtnVar.set(1)
        self.Rbtn1 = Radiobutton(root, 
                            variable=self.RbtnVar, 
                            value=1,
                            command = self.Rbtn1Click,
                            text="    :",
                            font=('Arial', 12)
                            )
        self.Rbtn1.grid(row=0,sticky=W)
        self.e1 = Entry(root,font=('Arial', 12), textvariable = self.var, width = 15)
        self.e1.grid(row=0, column=1,columnspan=2,sticky=E)
        self.e2 = Entry(root,font=('Arial', 12), textvariable = self.var1, width = 15)
        self.e2.grid(row=1, column=1,columnspan=2,sticky=E)

        self.Rbtn2 = Radiobutton(root, 
                            variable=self.RbtnVar, 
                            value=2,
                            command = self.Rbtn2Click,
                            text="     :",
                            font=('Arial', 12),
                            )
        self.Rbtn2.grid(row=2,sticky=W)

        self.btnOpen = Button(root, 
                            text = '    ',
                            state = DISABLED,
                            width = 14,
                            font = ('Arial', 12), 
                            command = self.openFile
                            )
        self.btnOpen.grid(row=2,column=1,columnspan=2,sticky=E)

        self.btnStartQuery = Button(root, text='    ',command=self.startQuery)
        self.btnStartQuery.grid(row=3, column=1,sticky=W)
        self.btnStopQuery = Button(root, text='    ',state = DISABLED, command=self.stopQuery)
        self.btnStopQuery.grid(row=3, column=2,sticky=E)

        self.text_msg = Text(root,height=10,width=30,font=('Arial', 12));
        self.text_msg.grid(row=4, columnspan=4,rowspan=2,sticky=W)

        self.root.update_idletasks()

    def Rbtn1Click(self):
        self.btnOpen['state'] = DISABLED
        self.e1['state'] = NORMAL
        self.e2['state'] = NORMAL

    def Rbtn2Click(self):
        self.btnOpen['state'] = NORMAL
        self.e1['state'] = DISABLED
        self.e2['state'] = DISABLED

    def openFile(self):
        import tkFileDialog 
        self.loadFileName = tkFileDialog.askopenfilename(
                            title = "        ",
                            filetypes=[('    ','*.txt')]
                            )

    def stopQuery(self):
        self.bStopQuery = True

    def startQuery(self):
        self.btnStartQuery['state'] = DISABLED
        self.btnStopQuery['state'] = NORMAL

        self.text_msg.delete(0.0, END)
        self.text_msg.update()

        #     
        if self.RbtnVar.get() == 1:
            strNum1 =self.var.get()
            strNum2 =self.var1.get()
        #      

        if self.RbtnVar.get() == 2:
            self.text_msg.insert(END,text1)
            self.text_msg.insert(END,text2)
            self.text_msg.update()            

            if self.bStopQuery:
                self.bStopQuery = True

        self.btnStartQuery['state'] = NORMAL
        self.btnStopQuery['state'] = DISABLED

def main():
    master = Tk()
    master.title("     ")
    # master.geometry('300x200')
    master.resizable(width=False, height=False)
    GUI_WINDOWS(master)

    master.mainloop()

if __name__ == '__main__':
    main()

参照先:
  • 英語http://www.tutorialspoint.com/python/python_gui_programming.htm
  • 中国語http://www.yiibai.com/python/python_gui_programming.html
  • Tkinter個のコントロールの具体的な応用例