小编Gre*_*106的帖子

Python 3.2输入日期函数

我想写一个函数,它接受用户输入的日期,用shelve函数存储它,并在调用后30天打印日期.

我想尝试一些简单的事情:

import datetime

def getdate():
    date1 = input(datetime.date)
    return date1

getdate()

print(date1)
Run Code Online (Sandbox Code Playgroud)

这显然不起作用.

我已经使用了上述问题的答案,现在我的程序部分正在运行!谢谢!现在是下一部分:

我正在尝试编写一个简单的程序,按照你指示我的方式获取日期并增加30天.

import datetime
from datetime import timedelta

d = datetime.date(2013, 1, 1)
print(d)
year, month, day = map(int, d.split('-'))
d = datetime.date(year, month, day)
d = dplanted.strftime('%m/%d/%Y')
d = datetime.date(d)+timedelta(days=30)
print(d)
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误:年,月,日= map(int,d.split(' - '))AttributeError:'datetime.date'对象没有属性'split'

最终我想要的是01/01/2013 + 30天并打印01/30/2013.

提前致谢!

python datetime input python-3.x

7
推荐指数
1
解决办法
5万
查看次数

实例化Python对象和使用列表

我是编程和尝试自学的新手.我目前正在尝试学习如何从类构建对象,我想我理解.我当前的任务是将对象添加到列表中并打印该列表.最终,我正在尝试构建一个创建对象的程序,并列出已在编号列表中创建的每个对象,即:

1 - tomato, red
2 - corn, yellow
etc...
Run Code Online (Sandbox Code Playgroud)

首先,我只是想构建这个的基本部分.这是我做的:

# Builds objects on instantiation for a vegetable and color
class Veg:
    def __init__(self, name, color):
        self.name = name
        self.color = color
        print('You have created a new', self.color, self.name, end='.\n')

# Function to create a new vegetable and store it in a list
def createVeg():
    name = input('What is the name of the Vegetable? ')
    color = input('What color is the vegetable? ')
    Veg(name, color)
    vegList.append(Veg)
    return

# Initialize …
Run Code Online (Sandbox Code Playgroud)

python class list python-3.x

4
推荐指数
1
解决办法
1215
查看次数

ttk 创建和使用自定义主题

我正在尝试将可定制的主题选择纳入我的程序中。我一直在参考本指南:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-themes.html,但我迷路了。

这是我一直用来尝试解决这个问题的示例代码。我正确地创建了一个新主题“影子”,但下一部分我正在努力解决。让我们保持简单,在“阴影”主题中,我想将框架背景更改为黑色,将按钮背景更改为海军蓝,将按钮前景更改为白色。

我该怎么做?

from tkinter import *
from tkinter import ttk

class Main:
    def __init__(self, master):
        self.master = master
        self.main_button = ttk.Button(self.master, text = 'New', command = self.new_window)
        self.main_button.grid()

    def new_window(self):
        pop_up = Top(self.master)

class Top:
    def __init__(self, master):
        pop_up = self.pop_up = Toplevel(master)
        self.pop_up_frame = ttk.Frame(pop_up, height = 100, width = 100)
        self.pop_up_frame.grid(sticky = E+W+S+N)
        self.s = ttk.Style()
        self.s.theme_create('shadow', parent = 'default')

        print(self.s.theme_names())

        self.c1_button = ttk.Button(pop_up, text = 'Default', command = self.get_default)
        self.c2_button = ttk.Button(pop_up, text = …
Run Code Online (Sandbox Code Playgroud)

themes tkinter ttk python-3.3

3
推荐指数
1
解决办法
4774
查看次数

标签 统计

python ×2

python-3.x ×2

class ×1

datetime ×1

input ×1

list ×1

python-3.3 ×1

themes ×1

tkinter ×1

ttk ×1