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

image.png

Python 全系列/第二阶段:Python 深入与提高/模块 2389楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2390楼
Python 全系列/第二阶段:Python 深入与提高/模块 2391楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2392楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 2394楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2395楼

代码:

"""测试一个grid布局"""

from tkinter import *
from tkinter import messagebox

class Application(Frame):
    """一个经典的GUI程序的类的写法"""
    def __init__(self, master=None):
        super().__init__(master)  # super()代表的是父类的定义,而不是父类对象
        self.master = master
        self.pack()
        self.createWidget()
    def createWidget(self):
        """创建计算器界面的组件"""
        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, pady=10)
        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("190x235+1300-400")
    app = Application(master=root)
    root.mainloop()

疑问01.png

老师,我按照视频上的代码敲了一遍,为什么0没有跨列呢?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2396楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2398楼
Python 全系列/第二阶段:Python 深入与提高/模块 2399楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2400楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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