用python写一个获取当前时间的app
时间: 2023-07-01 15:22:38 浏览: 58
好的,以下是一个获取当前时间的Python应用程序的代码:
```python
import datetime
now = datetime.datetime.now()
print("当前时间是:")
print(now.strftime("%Y-%m-%d %H:%M:%S"))
```
这个应用程序使用Python的datetime模块来获取当前时间,并使用strftime函数将其格式化为可读性更好的字符串。
相关问题
用Python写一个获取app句柄的代码
可以使用Python中的`win32gui`模块来获取app的句柄。以下是一个获取指定窗口句柄的示例代码:
```python
import win32gui
# 定义一个回调函数,用于找到指定类名和窗口名的窗口
def find_window_handler(hwnd, window_name):
if win32gui.IsWindowVisible(hwnd) and window_name in win32gui.GetWindowText(hwnd):
print("找到窗口:{}".format(win32gui.GetWindowText(hwnd)))
return hwnd
# 查找窗口句柄
hwnd = win32gui.EnumWindows(find_window_handler, "窗口标题")
```
上述代码中,`find_window_handler`是一个回调函数,用于判断当前枚举到的窗口是否为指定窗口。`EnumWindows`函数会遍历当前系统中所有的顶级窗口,并对每个窗口调用`find_window_handler`函数。如果找到了指定窗口,则返回窗口句柄。
在上述代码中,我们将窗口标题作为参数传递给`find_window_handler`函数。如果你要获取的是其他信息,如窗口类名、进程ID等,可以在`find_window_handler`函数中使用`win32gui`模块提供的相应API函数来获取。
用python写一个mock,返回当前长沙的天气
由于mock实际上是模拟一个接口返回固定的数据,而获取天气需要调用第三方天气接口,因此以下代码实际上是调用第三方天气接口,而不是真正的mock。
```python
import requests
def get_weather():
url = 'https://tianqiapi.com/api?version=v6&appid=YOUR_APPID&appsecret=YOUR_APPSECRET&cityid=101250101'
response = requests.get(url)
data = response.json()
return data
weather_data = get_weather()
print('当前天气:', weather_data['wea'])
```
需要将YOUR_APPID和YOUR_APPSECRET替换为自己在https://www.tianqiapi.com/apply/申请到的appid和appsecret。同时,cityid可以根据需要更改为其他城市的id。