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

from tkinter import *
from tkinter import messagebox
import random

win_width=450
win_height=300
class Application(Frame):
    def __init__(self,master):
        super().__init__(master)
        self.master = master
        self.pack()
        # label组建:小姐姐真漂亮,我喜欢你,图片
        self.label01 = Label(root, text="小姐姐真漂亮,我喜欢你\n做我女朋友好吗?"
                             , font=("KaiTi", 17), width=27, height=2, fg="red")
        self.label01.place(x=0, y=0)
        global photo
        photo = PhotoImage(file="e:/python图片/AKAKA.gif")
        self.label02 = Label(root, image=photo)
        self.label02.place(x=200, y=60)

        # 设置按钮好哒、不愿意
        self.but01 = Button(root, text="好哒", font=("KaiTi", 28), command=self.haoda, activebackground="blue")
        self.but01.place(x=60,y=70)
        self.but02 = Button(root,text="不愿意", font=("KaiTi", 15))
        self.but02.place(x=60,y=170)

        self.but02.bind("<Enter>",self.buyuanyi)

    def buyuanyi(self, event):
        x1 = random.randrange(int(win_width)-50)
        y1 = random.randrange(int(win_height)-50)
        print(x1, y1)
        self.but02.place(x=x1, y=y1)

    def haoda(self):
        messagebox.showinfo("宝贝", "属于我们的甜甜的恋爱开始啦")
        root.destroy()


if __name__ == '__main__':
    root = Tk()
    root.title("I LOVE U")
    root.geometry(str(win_width)+"x"+str(win_height)+"+700+400")
    app = Application(root)
    root.mainloop()

老师,为啥这些地方是root,不能是self,我用了self,就显示不出来了?417236ae20f6b9145b984759fc5ab06.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2717楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2719楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2720楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2721楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2723楼

from tkinter import *


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

        self.createWight()

    def createWight(self):
        btnTEXT = (("MC", "M+", "M-", "MR"),
                   ("C", "±", "/", "*"),
                   (7,8,9,"-"),
                   (4,5,6,"+"),
                   (0,1,2,"="),
                   (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=1+rindex,column=cindex, rowspan=2,sticky=NSEW)
                elif c == "0":
                    Button(self,text=c,width=2).grid(row=1+rindex,column=cindex, columnspan=2, sticky=NSEW)
                elif c == ".":
                    Button(self,text=c,width=2).grid(row=1+rindex,column=cindex+1, sticky=NSEW)
                else:
                    Button(self,text=c,width=2).grid(row=1+rindex,column=cindex,sticky=NSEW)



if __name__ == '__main__':
    root = Tk()
    root.title("计算器")
    root.geometry("300x300+200+300")
    app = Application(master=root)
    root.mainloop()

老师,和您的一样,0为啥没有跨列

121ef5b2129d5061242c87b7009e7f1.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2725楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2726楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2727楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2728楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2729楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2730楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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