我正在编写一个小程序,其目的是在窗口大小更改时运行调整大小方法。为此,我使用了toplevel.bind("<Configure>", self.resize). 虽然这确实适用于调整大小,但该方法会被调用数百次:滚动时、使用某些按钮时,即使窗口大小不会因任何这些操作而改变。如何绑定事件,以便仅在更改窗口大小时调用它?
import tkinter as tk
def resize(event):
print("widget", event.widget)
print("height", event.height, "width", event.width)
parent = tk.Toplevel()
canvas = tk.Canvas(parent)
scroll_y = tk.Scrollbar(parent, orient="vertical", command=canvas.yview)
scroll_x = tk.Scrollbar(parent, orient="horizontal", command=canvas.xview)
frame = tk.Frame(canvas)
# put the frame in the canvas
canvas.create_window(0, 0, anchor='nw', window=frame)
# make sure everything is displayed before configuring the scrollregion
canvas.update_idletasks()
canvas.configure(scrollregion=canvas.bbox('all'),
yscrollcommand=scroll_y.set,
xscrollcommand=scroll_x.set)
scroll_y.pack(fill='y', side='right')
scroll_x.pack(fill='x', side='bottom')
canvas.pack(fill='both', expand=True, side='left')
parent.bind("<Configure>", resize)
parent.mainloop()
Run Code Online (Sandbox Code Playgroud)
输出:
widget .!toplevel
height 200 width 200
widget .!toplevel.!scrollbar …Run Code Online (Sandbox Code Playgroud) 当我尝试按原样运行此代码时,出现此错误“IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, 'CoInitialize 尚未被调用。', None, None )" ,但是如果我单独运行 stp_tracker ,它工作得很好,如果我单独运行 notify stp ,它工作得很好。我感谢任何人的意见。谢谢
import time
import win32com.client
# import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# import watchdog
class MyHandler(PatternMatchingEventHandler):
patterns = ["*.stp", "*.step", "*.txt"]
def process(self, event):
"""
event.event_type
'modified' | 'created' | 'moved' | 'deleted'
event.is_directory
True | False
event.src_path
path/to/observed/file
"""
# the file will be processed there
print(event.src_path, event.event_type)
def on_modified(self, event):
self.process(event)
notify_stps()
def on_created(self, event): …Run Code Online (Sandbox Code Playgroud) 我想将音频文件从一种格式转换为另一种格式,所以我使用了 Pydub AudioSegment。
AudioSegment.from_file(input_filename, 'mp4')
Run Code Online (Sandbox Code Playgroud)
python 脚本文件运行良好,但是当我使用 pyinstaller 将其捆绑为可执行文件时,出现以下错误。
我通读了 Github 问题页面并实施了那里建议的解决方案,但这似乎不起作用。我已将 ffmpeg.exe 和 ffprobe.exe 放在我的项目目录中,并使用 AudioSegment.converter 设置路径
Traceback (most recent call last):
File "main.py", line 91, in change_format
converted_audio = AudioSegment.from_file(input_filename, 'mp4')
File "lib\site-packages\pydub\audio_segment.py", line 685, in from_file
File "lib\site-packages\pydub\utils.py", line 274, in mediainfo_json
File "subprocess.py", line 728, in __init__
File "subprocess.py", line 1025, in _get_handles
OSError: [WinError 6] The handle is invalid
Run Code Online (Sandbox Code Playgroud)
请提出解决此问题的方法。
我想使用IFileOperation从 python 代码复制文件 -
在 Windows 10 上,Python 3.8 -
import ctypes
ctypes.windll.shell32.IFileOperation
Run Code Online (Sandbox Code Playgroud)
似乎不存在。
我怎样才能使用IFileOperation(不是已弃用的SHFileOperationAPI)ctypes?
这是我的所有代码,我尝试用它来解密 Windows 中的 chrome 密码。
\n\nimport os\nimport sqlite3\nimport win32crypt\ndef get_chrome():\n data_path = os.path.expanduser('~') + r'\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Login Data'\n c = sqlite3.connect(data_path)\n cursor = c.cursor()\n select_statement = 'SELECT origin_url, username_value, password_value FROM logins'\n cursor.execute(select_statement)\n login_data = cursor.fetchall()\n\n cred = {}\n\n string = ''\n\n for url, user_name, pwd in login_data:\n pwd = win32crypt.CryptUnprotectData(pwd)\n cred[url] = (user_name, pwd[1].decode('utf8'))\n string += '\\n[+] URL:%s USERNAME:%s PASSWORD:%s\\n' % (url,user_name,pwd[1].decode('utf8'))\n print(string)\n\n\nif __name__=='__main__':\n get_chrome()\nRun Code Online (Sandbox Code Playgroud)\n\n它显示的错误是:
\n\npywintypes.error: (87, 'CryptProtectData', 'Param\xc3\xa8tre incorrect.') when i'm trying to decrypt chrome …Run Code Online (Sandbox Code Playgroud) 我正在 Codecademy 上开展一个简单的项目,并希望确保我走在正确的轨道上。然而,当我尝试调试代码时,弹出一个简单的 Google 页面,然后读取错误
错误:

我对编码很陌生,所以我不知道从哪里开始。
我正在使用以下代码从图像生成 PDF。
PDF=pytesseract.image_to_pdf_or_hocr(test_image,lang='dan',config='',nice=0,extension='pdf')
Run Code Online (Sandbox Code Playgroud)
并且 PDF 变量的类型显示为 BYTES。
我如何发布或生成 PDF?
我使用pyinstaller打包一个exe文件。
在我的电脑(Win 10)上可以正常运行。
但在别人的电脑(Win 7)上却显示
__main__.pyinstallerimporterror failed to load dynlib/dll "shcore".
Run Code Online (Sandbox Code Playgroud)
关于shcore,我用
import ctypes
PROCESS_PER_MONITOR_DPI_AWARE = 2
ctypes.windll.shcore.SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE)
Run Code Online (Sandbox Code Playgroud)
我应该怎么办?提前致谢。
一个简单的小问题:
exec("a=3")
print(a)
# This will print 3
Run Code Online (Sandbox Code Playgroud)
如果我使用这个:
def func():
exec("a=3")
print(a)
func()
# NameError: name 'a' is not defined.
Run Code Online (Sandbox Code Playgroud)
发生了什么?我如何exec()在函数中为它赋值?
编辑:我发现了一个有同样问题的问题,但仍然没有解决。
你为什么要这么做?
我知道使用exec()是不好的并且不安全。但是最近我尝试解决OP的问题。我遇到了它。
我正在使用python 3.6并尝试import win32com.client使用win32Outlook 发送电子邮件,但不断收到上述内容import error。
我已经安装了pywin32和pypiwin32并运行了postinstall脚本(确实看到它说“您没有安装 COM 对象的权限。)
我在运行脚本的位置看到了pywintpyes36.dll和。pyhoncom36.dll
我做了一些搜索,似乎找不到我丢失的东西。是因为我无法安装 COM 对象吗?任何帮助将不胜感激,谢谢。
[编辑]
我正在使用 venv,并使用py -m pip install pywin32和安装了两个库py -m install pypiwin32。我确认我在 venv 的“site-packages”文件夹中看到了安装。
我的进口:
import sys
sys.path.append("C:\path\venv\Lib\site-packages")
import tkinter as tk
import getpass
import os.path
import time
import os
import win32com.client as win32
import sqlite3
from datetime import datetime
from functools import partial
Run Code Online (Sandbox Code Playgroud)
我那里有sys.path.append,否则根本找不到该模块。
完整错误消息:
Traceback …Run Code Online (Sandbox Code Playgroud) 我有一个令我烦恼的问题。我目前正在使用 Tkinter GUI 构建一个小型应用程序。
在首页上,我想要一些文本或滚动文本小部件中的介绍性文本。我遇到的代码示例使用 INSERT、CURRENT 和 END 等关键字在小部件内进行索引。
我已将以下代码逐字复制粘贴到编辑器中,但它无法识别 INSERT(抛出错误:“NameError:名称'INSERT'未定义”):
import tkinter as tk
from tkinter import scrolledtext
window = tk.Tk()
window.title("test of scrolledtext and INSERT method")
window.geometry('350x200')
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.insert(INSERT,'You text goes here')
txt.grid(column=0,row=0)
window.mainloop()
Run Code Online (Sandbox Code Playgroud)
如果我将 [INSERT] 更改为 [1.0],我可以使代码正常工作,但令人非常沮丧的是,我无法使 INSERT 正常工作,因为我在遇到的每个示例代码中都看到了这一点
set我尝试测试和list之间的速度tuple得到了令人惊讶的结果。
在此之前,我知道这比基于此答案set更快list。
这是我的测试代码:
\n\nimport timeit,time\nfrom sys import getsizeof as Size\n\nList_Test = [range(1000)]\nprint("The Size of List is : {}".format(Size(List_Test)))\nSet_Test = set(range(1000))\nprint("The Size of Set is : {}".format(Size(Set_Test)))\nTuple_Test = tuple(range(1000))\nprint("The Size of Tuple is : {}".format(Size(Tuple_Test)))\n\n\nprint("\\nNow is to test speed\\n")\ntime.sleep(3)\n\ndef Create_List():\n List = [i for i in range(1000)]\n\ndef Test_List():\n for i in List_Test:\n if i == 6:\n break\n\ndef Create_Set():\n Set = set(i for i in range(1000))\n\ndef Test_Set():\n for i …Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×5
pywin32 ×4
winapi ×4
events ×2
pyinstaller ×2
tkinter ×2
ctypes ×1
debugging ×1
go ×1
goland ×1
html ×1
list ×1
ocr ×1
pydub ×1
python-3.7 ×1
set ×1
sqlite ×1
tkinter-text ×1
watchdog ×1