Python3记事本UI

代码:(半成品)

#encoding:utf-8
import tkinter
# 菜单
menuText = {"文件": ["新建", "打开", "保存", "另存为...", "退出"],
            "编辑": ["查找", "替换", "查找下一个..."], "运行": ["运行", "调试"], "帮助": ["关于"]}
# notepad类
class notepad(tkinter.Tk):
    def __init__(self, width=0, height=0, adjustX=0, adjustY=0, flag=False):
        tkinter.Tk.__init__(self)
        if flag:
            self.width = width
            self.height = height
        else:
            self.width = self.winfo_screenwidth()
            self.height = self.winfo_reqheight()
        self.adjustX = adjustX
        self.adjustY = adjustY
        self.start()
    # 屏幕位置确定
    def screenCenter(self):
        screenHeight = self.winfo_screenheight()
        screenWidth = self.winfo_screenwidth()
        centerX = (screenWidth - self.width) / 2 - self.adjustX
        centerY = (screenHeight - self.height) /2 - self.adjustY
        centerPlay = str(self.width) + 'x' + str(self.height) + '+' + str(int(centerX)) + '+' + str(int(centerY))
        self.geometry(centerPlay)
    # notepad菜单
    def notepadMenu(self):
        menubar = tkinter.Menu(self)
        for menuItem in menuText:
            menu = tkinter.Menu(menubar, tearoff=0)
            for text in menuText[menuItem]:
                menu.add_command(label=text, command=lambda name=text:self.methodFunc(name))
            menubar.add_cascade(label=menuItem, menu=menu)
        self.config(menu=menubar)
    # notepad启动
    def start(self):
        self.title("notepad")
        self.screenCenter()
        self.notepadMenu()
        text = tkinter.Text(self)
        text.pack(fill=tkinter.BOTH)
    # notepad菜单及命令设置函数
    def methodFunc(self, name):
        if name == "退出":
            self.quit()
        # 下面方法省略
# 运行
if __name__ == '__main__':
    # notepad实例化
    note = notepad(width=500, height=300, adjustY=100, flag=True).mainloop()

图片:
Python3记事本UI_第1张图片

你可能感兴趣的:(Python)