会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133306个问题
Python 全系列/第二阶段:Python 深入与提高/模块 2836楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2837楼

"""计算机软件界面的设计"""
from tkinter import *
from tkinter import messagebox

class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        """通过grid布局实现计算机界面"""
        btnText = (("MC","M+","M-","MR"),
                   ("C","±","÷","×"),
                   (7,8,9,"-"),
                   (4,5,6,"+"),
                   (1,2,3,"="),
                   (0,"."))
        Entry(self).grid(row=0,column=0,columnspan=4)
        for rindex,r in enumerate(btnText):
            for cindex,c in enumerate(r):
                if c == "=":
                    Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex, rowspan=2, sticky=NSEW)
                elif c == 0:
                    Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex, columnspan=2, sticky=NSEW)
                elif c == ".":
                    Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex + 1, sticky=NSEW)
                else:
                    Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex, sticky=NSEW)

if __name__ =="__main__":
    root = Tk()
    root.geometry("200x200+200+300")
    app = Application(master=root)
    root.mainloop()


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2838楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2841楼
Python 全系列/第二阶段:Python 深入与提高/模块 2842楼

#encoding=utf-8

from tkinter import *
from tkinter.colorchooser import *
from tkinter.filedialog import *

class Application(Frame):
    def __init__(self, master = None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()
    def createWidget(self):
        # 创建主菜单栏
        menubar = Menu(root)

        # 创建子菜单
        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        # 将子菜单加入到主菜单栏
        menubar.add_cascade(label='文件(F)', menu=menuFile)
        menubar.add_cascade(label='编辑(E)', menu=menuEdit)
        menubar.add_cascade(label='帮助(H)', menu=menuHelp)

        # 添加菜单项
        menuFile.add_command(label='新建', accelerator='ctrl + n', command=self.test)
        menuFile.add_command(label='打开', accelerator='ctrl + o', command=self.test)
        menuFile.add_command(label='保存', accelerator='ctrl + s', command=self.test)
        menuFile.add_separator()  # 添加分割线
        menuFile.add_command(label='退出', accelerator='ctrl + q', command=self.test)

        # 将主菜单栏加到跟窗口
        root['menu'] = menubar

        # 创建上下文菜单
        menubar2 = Menu(root)
        menubar2.add_command(label='颜色', command = self.openAskColor)

        menuedit = Menu(menubar2, tearoff = 0)
        menuedit.add_command(label='剪切')
        menuedit.add_command(label='复制')
        menuedit.add_command(label='粘贴')

        menubar2.add_cascade(label='编辑', menu=menuedit)

        # 编辑区
        w1 = Text(root, width=50, height=30)
        w1.pack()
        w1.bind('<Button-3>', text)
    def test(self):
        pass

    def openAskColor(self):
        s1 = askcolor(color='red', title='选择背景色')
        root.config(bg=s1[1])

    def test1(event):
        # 菜单在鼠标右键单击的坐标处显示
        menubar2.post(event.x_root, event.y_root)


if __name__ == "__main__":
    root = Tk()
    root.title('my window');root.geometry('450x300')
    app = Application(master = root)
    root.mainloop()

老师:问题很多,视频七分钟,百度两小时

  1. 如图,菜单栏虚线可以点击,并且点击后新弹出个窗口

image.png

2.这些地方是调用组件么。组件第一个字母不都是大写

image.png


3.我这里总是显示这样的,不知道为什么

image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2843楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2844楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2848楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 2849楼

tkinter.filedialog *
tkinter.colorchooser *
tkinter *
Application(Frame):

    (master=):
        ().(master)        .master = master
        .textpad = .pack()
        .createWidget()

    ():
        menubar = Menu(root)

        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        menubar.add_cascade(==menuFile)
        menubar.add_cascade(==menuEdit)
        menubar.add_cascade(==menuHelp)

        menuFile.add_command(===.test)
        menuFile.add_command(===.openfile)
        menuFile.add_command(===.savefile)
        menuFile.add_separator()  menuFile.add_command(===.exit)

        root[] = menubar


        .textpad = Text(root==)
        .textpad.pack()

        .contextMenu = Menu(root)
        .contextMenu.add_command(==.test)

        root.bind(.createContextMenu)

    ():
        .textpad.delete()
        askopenfile(=) f:
            .textpad.insert(INSERTf.read())
            .filename=f.name

    ():
        (.filename) f:
            c=.textpad.get(END)
            f.write(c)
    ():
        root.quit()

    ():
        (event):
        .contextMenu.post(event.x_rootevent.y_root)


__name__ == :
    root = Tk()
    root.geometry()
    root.title()
    app = Application(=root)
    root.mainloop()

老师,我这个txt文件打不开

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2850楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637