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

from tkinter import *


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

    def creatWidget(self):
        '''通过 grid 布局实现简易计算器'''
        btntext = (('MC', 'M+', 'M-', 'MR'),
                   ('C', '±', '÷', '×'),
                   (7, 8 , 9 , '-'),
                   (4, 5, 6, '+'),
                   (1, 2, 3, '='),
                   (0, '.'))
        self.entry01 = Entry(self)
        self.entry01.grid(row=0, columnspan=4, pady =10)
        # pady 表示距下面的按钮 10 个像素

        for rindex,r in enumerate(btntext):
            for cindex,c in enumerate(r):
                if c == '=':
                    Button(self, text=c, width=2).grid(rows=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('170x230+200+200')
    root.title('简易计算器界面')
    app = Application(master=root)
    root.mainloop()


运行结果:

image.png


问题:

老师,我检查了很久,没有检查出来这个  ’=‘ 为什么没有跨行,请帮忙看一下,谢谢~

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1918楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1919楼
Python 全系列/第二阶段:Python 深入与提高/模块 1920楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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