學python3編程gui用tkinter示例

學python編程gui用tkinter示例

#!/usr/bin/python3
# -*- coding:utf-8 -*-

from tkinter import *
from tkinter.ttk import *


class Application_ui(Frame):
    # 這個類僅實現界面生成功能,具體事件處理代碼在子類Application中。
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title('使用vb6插件生成界面')
        self.master.geometry('312x206')
        self.createWidgets()

    def createWidgets(self):
        self.top = self.winfo_toplevel()

      #  self.style = Style()

        self.Command2Var = StringVar(value='退出')
        self.Command2 = Button(self.top, text='退出', textvariable=self.Command2Var, command=self.Command2_Cmd)
        self.Command2.setText = lambda x: self.Command2Var.set(x)
        self.Command2.text = lambda: self.Command2Var.get()
        self.Command2.place(relx=0.538, rely=0.66, relwidth=0.337, relheight=0.238)

        self.Command1Var = StringVar(value='測試')
        self.Command1 = Button(self.top, text='測試', textvariable=self.Command1Var, command=self.Command1_Cmd)
        self.Command1.setText = lambda x: self.Command1Var.set(x)
        self.Command1.text = lambda: self.Command1Var.get()
        self.Command1.place(relx=0.128, rely=0.66, relwidth=0.337, relheight=0.238)

        self.Text1Var = StringVar(value='Text1')
        self.Text1 = Entry(self.top, textvariable=self.Text1Var)
        self.Text1.setText = lambda x: self.Text1Var.set(x)
        self.Text1.text = lambda: self.Text1Var.get()
        self.Text1.place(relx=0.256, rely=0.272, relwidth=0.439, relheight=0.238)

        self.Label1Var = StringVar(value='Label1')
       # self.style.configure('TLabel1.TLabel', anchor='w'), style='TLabel1.TLabel'
        self.Label1 = Label(self.top, text='Label1', textvariable=self.Label1Var)
        self.Label1.setText = lambda x: self.Label1Var.set(x)
        self.Label1.text = lambda: self.Label1Var.get()
        self.Label1.place(relx=0.231, rely=0.078, relwidth=0.49, relheight=0.16)


class Application(Application_ui):
    # 這個類實現具體的事件處理回調函數。界面生成代碼在Application_ui中。
    def __init__(self, master=None):
        Application_ui.__init__(self, master)

    def Command2_Cmd(self, event=None):
        #sys.exit()
        self.quit()

    def Command1_Cmd(self, event=None):
        self.Label1Var.set(self.Text1Var.get())


if __name__ == "__main__":
    top = Tk()
    Application(top).mainloop()

和其他界面庫相比,自帶的tkinter庫做出來體積最小,簡單自用還是合適的,寫界面代碼比較方便的就是利用vb6的tkinter-designer插件,去網站看詳細tkinter-designer說明

在vb6裏照原先vb6的exe方法畫界面,畫完點擊插件,就生成py代碼,將這個代碼複製到需要的python工程裏即可

缺點就是vb6是windows的應用,不能跨平臺,需要切換一下系統,或者裝個虛擬機加載一下xp之類小的win系統也可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章