会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134169个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 21933楼
JAVA 全系列/第十六阶段:分布式文件存储与数据缓存/MongoDB 21934楼

# encoding=utf-8
from tkinter import Tk, Frame, Menu, Text, INSERT, END
from tkinter.filedialog import *
from tkinter.colorchooser import *


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

    def createWidget(self):
        menubar = Menu(root)

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

        menubar.add_cascade(label='file(F)', menu=menuFile)
        menubar.add_cascade(label='edit(E)', menu=menuEdit)
        menubar.add_cascade(label='help(H)', menu=menuHelp)

        menuFile.add_command(label='new', accelerator='ctrl+n', command=self.newfile)
        menuFile.add_command(label='open', accelerator='ctrl+o', command=self.openfile)
        menuFile.add_command(label='save', accelerator='ctrl+s', command=self.savefile)
        menuFile.add_separator()
        menuFile.add_command(label='exit', accelerator='ctrl+q', command=self.exit)

        root['menu'] = menubar

        root.bind('<Control-n>', lambda event: self.newfile())
        root.bind('<Control-o>', lambda event: self.openfile())
        root.bind('<Control-s>', lambda event: self.savefile())
        root.bind('<Control-q>', lambda event: self.exit())

        self.textpad = Text(root, width=50, height=30)
        self.textpad.pack()

        self.contextMenu = Menu(root)
        self.contextMenu.add_command(label='background color', command=self.openAskColor)

        root.bind('<Button-3>', self.createContextMenu)

    def newfile(self):
        self.textpad.delete('1.0', 'end')
        self.filename = asksaveasfilename(title='save as', initialfile='unnamed.txt',
                                          filetypes=[('text document', '*txt')],
                                          defaultextension='.txt')
        self.savefile()

    def openfile(self):
        self.textpad.delete('1.0', 'end')
        with open(askopenfile(title='open file')) as f:
            self.textpad.insert(INSERT, f.read())
            self.filename = f.name

    def savefile(self):
        with open(self.filename, 'w') as f:
            c = self.textpad.get(1.0, END)
            f.write(c)

    def exit(self):
        root.quit()

    def openAskColor(self):
        s1 = askcolor(color='red', title='choose colors')
        self.textpad.config(bg=s1[1])

    def createContextMenu(self, event):
        self.contextMenu.post(event.x_root, event.y_root)


if __name__ == '__main__':
    root = Tk()
    root.geometry('450x300+200+300')
    root.title('My notebook')
    app = Application(master=root)
    root.mainloop()

老师您好,在这一个视频中我也出现了好多问题,根据其他同学的提问,老师们的解答,和百度知乎上解决了一部分,到了这一步没找到解决方法:就是打包时一直提示:

(venv) C:\Users\刘琪\PycharmProjects\GUI>pyinstaller -F mytest.py

failed to create process.

麻烦老师解答,谢谢


Python全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 21935楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 21936楼
JAVA 全系列/第六阶段:JavaWeb开发/JSP技术详解(旧) 21937楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 21938楼
JAVA 全系列/第五阶段:Web全栈开发基础与Vue/Javascript 语言(旧) 21940楼
JAVA 全系列/第五阶段:Web全栈开发基础与Vue/Javascript 语言(旧) 21941楼
JAVA 全系列/第二十三阶段:分布式医疗云平台/系统管理前后端开发(旧) 21942楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于SpringSecurity实现后台登录功能 21943楼

class STDYLTSW:


    def __init__(self, year, month, day):
        # 记录当前日期 的三个属性
        self.day = day
        self.month = month
        self.year = year
        self.date = (1990, 1, 1)  # 初始日期
        self.days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]  # 跟上月的天数之差
        self.totalDays = 0


    def judge_year(self):
        # 判断闰年 是 返回1  否返回0
        if (self.year % 4 == 0 and self.year % 100 != 0) or (self.year % 400 == 0):
            return 1
        else:
            return 0

    def cal_total_days(self):
        # 计算年和年之间的天数之差
        t = self.date[0]  # 将1990赋给t
        while t < self.year:
            if self.judge_year() == 1:
                self.totalDays += 366
            else:
                self.totalDays += 365
            t = t + 1
        # 计算月和月之间的天数之差
        i = 0
        if self.judge_year() == 1:
            self.days[2] += 1
        while i < self.month:
            self.totalDays += self.days[i]
            i += 1
        # 计算最后天数之差
        self.totalDays += self.day

    def show_info(self):
        print("{}/{}/{}与1990/1/1天数之差是{}天".format(self.year, self.month, self.day, self.totalDays))
        result = self.totalDays % 5
        # 判断是打鱼还是晒网
        if 0 < result < 4:
            print("今天打鱼")
        else:
            print("今天晒网!")


y, m, d = [int(i) for i in input().split()]
p = STDYLTSW(y, m, d)
p.cal_total_days()
p.show_info()

三天打鱼两天晒程序,用面向对象的方式编写,结果老是多了几天?不知道为什么?

以上是面向对象的代码?

面向过程的正确代码如下:

# 判断是否是闰年 是的返回1 否返回0
def run_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return 1
    else:
        return 0

def count_day(currentDay):
    perMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
    totalDay = 0
    year = 1990
    while year < currentDay['year']:
        # 求出指定日期之前的每一年的天数之和
        if run_year(year) == 1:
            totalDay += 366
        else:
            totalDay += 365
        year += 1
    if run_year(currentDay['year']) == 1:
        perMonth[2] += 1
    # 如果是闰年,二月份是29天
    i = 0
    while i < currentDay['month']:
        # 将本年的天数累加到totalDay中
        totalDay += perMonth[i]
        i += 1
    totalDay += currentDay['day']
    return totalDay


if __name__ == '__main__':
    # while True:
    print("输入指定日期包括年、月、日。如:1999 1 31")
    year, month, day = [int(i) for i in input().split()]
    # 定义一个日期字典
    today = {'year': year, 'month': month, 'day': day}
    totalDay = count_day(today)
    # 求出指定日期距离1990年1月1日的天数
    print("{}年{}月{}日与1990年1月1日相差{}天".format(year, month, day, totalDay))
    # 天数%5 判断输出打鱼还是晒网
    result = totalDay % 5
    if 0 < result < 4:
        print("今天打鱼!")
    else:
        print("今天晒网!")

麻烦老师解答一下。

Python全系列/第一阶段:AI驱动的Python编程/Python入门(动画版) 21945楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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