我正在尝试制作一个小部件来保存一个图像,该图像会自动调整大小以适应其容器,例如,如果直接打包到窗口中,那么扩展该窗口将扩展图像.
我有一些半功能的代码,但我必须在其中一个例程中添加一些常量,以防止自动调整大小重新触发自身(导致它继续增长)
我确定原因是由于小部件内部填充/边框,但即使尝试考虑到这一点我得到了这个问题.
我在64位Windows 7上使用python 3.3.2和PIL 1.1.7,我的代码如下:
from tkinter import tix
from PIL import Image, ImageTk
def Resize_Image(image, maxsize):
r1 = image.size[0]/maxsize[0] # width ratio
r2 = image.size[1]/maxsize[1] # height ratio
ratio = max(r1, r2)
newsize = (int(image.size[0]/ratio), int(image.size[1]/ratio)) # keep image aspect ratio
image = image.resize(newsize, Image.ANTIALIAS)
return image
class Pict_Frame(tix.Label):
def __init__(self, parent=None, picture=None, maxupdate=None, **kwargs):
tix.Label.__init__(self, parent, **kwargs)
self.bind("<Configure>", self._resize_binding)
self.maxupdate = maxupdate
self.update_after_id = None
self.photo = None
self.image = None
if picture:
self.set_picture(picture)
def _resize_binding(self, …
Run Code Online (Sandbox Code Playgroud) 我有一个Android电视(飞利浦49PUS6401)我想连接一个环绕声系统,它比内置扬声器质量好得多,即使只用于立体声.
然而,这台电视分离主输出和耳机输出的音量控制,结果是即使关闭电视扬声器,遥控器上的音量键也不会调整耳机音量(我用作一条线路)出)
我打算写一个简单的应用程序,以便在桅杆音量改变时调整耳机音量,但我似乎无法弄清楚如何获取或设置耳机音量,因为音频管理器处理流而不是输出,所以我能从中得到的唯一价值就是主人.
我如何找到获取/设置耳机音量的方法?我通过adb获得了shell访问权限,但我无法获得root权限.
作为参考,电视正在运行最新的固件,这是Android 5.1.1
我正在尝试扩展 ttk 组合框类以允许自动建议。我的代码运行良好,但我想让它在输入一些文本后显示下拉列表,而无需从小部件的输入部分移除焦点。
我正在努力解决的部分是找到一种强制下拉的方法,在 python 文档中我找不到任何提及这一点,但是在 tk 文档中我确实找到了一个我认为应该这样做的 post 方法,除非它没有t 似乎是在 python 包装器中实现的。
我还尝试在自动建议发生后生成向下箭头键事件,但是虽然这确实显示了下拉列表,但它会移除焦点,并且在此事件之后尝试设置焦点似乎也不起作用(焦点不返回)
有没有人知道我可以用来实现这一目标的功能?
我拥有的代码仅适用于仅使用标准库的 python 3.3:
class AutoCombobox(ttk.Combobox):
def __init__(self, parent, **options):
ttk.Combobox.__init__(self, parent, **options)
self.bind("<KeyRelease>", self.AutoComplete_1)
self.bind("<<ComboboxSelected>>", self.Cancel_Autocomplete)
self.bind("<Return>", self.Cancel_Autocomplete)
self.autoid = None
def Cancel_Autocomplete(self, event=None):
self.after_cancel(self.autoid)
def AutoComplete_1(self, event):
if self.autoid != None:
self.after_cancel(self.autoid)
if event.keysym in ["BackSpace", "Delete", "Return"]:
return
self.autoid = self.after(200, self.AutoComplete_2)
def AutoComplete_2(self):
data = self.get()
if data != "":
for entry in self["values"]:
match = True
try:
for index in range(0, …
Run Code Online (Sandbox Code Playgroud) 对于我的一些 python 应用程序,我使用 ctypes 和 Windows API 安装系统钩子,为了让它们工作,必须运行消息循环。因为我在前台发生了其他事情,所以这个消息循环被设置为作为后台线程运行。这部分工作正常,但是当我想停止这个后台线程时,我无法让它退出,当收到 WM_QUIT 消息时,GetMessage 函数应该返回 0,并且我的消息循环的主体应该返回 0,但由于某种原因它永远不会返回。我尝试过 SendMessage、PostMessage、PostThreadMessage 和 PostQuitMessage 的各种组合,但没有成功。
#!python3
import tkinter as tk
import ctypes
import ctypes.wintypes
from threading import Thread
user32 = ctypes.windll.user32
def loop():
msg = ctypes.wintypes.MSG()
while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
print('msg')
user32.TranslateMessageW(msg)
user32.DispatchMessageW(msg)
print('end of loop')
def SendMessage1():
user32.SendMessageA(0xFFFF, 0x001C, 0, 0) # broadcast handle, app_activate message
def SendMessage2():
user32.SendMessageA(0, 0x001C, 0, 0) # null handle
def PostMessage1():
user32.PostMessageA(0xFFFF, 0x001C, 0, 0)
def PostMessage2():
user32.PostMessageA(0, …
Run Code Online (Sandbox Code Playgroud) I'm currently working on a DLL that needs to convert back and forth between the friendly name for a value and the value itself. As this code is used in many places throughout the codebase, I want to try and keep it simple and in a single function or object so I only have to declare them once.
From my reading it looks like CMap
is the tool for the job, but I can't seem to discover any combination of …
我正在尝试在滚动文本框的底部添加超链接样式覆盖,以便在用户将鼠标移到超链接上时显示状态栏类型标签(如webbrowser在左下角显示链接地址).我开始使用effbot中的超链接管理器代码并对其进行修改以添加回调以设置文本的位:
try:
from tkinter import scrolledtext
except ImportError:
import ScrolledText as scrolledtext
class HyperlinkManager(object):
"""A class to easily add clickable hyperlinks to Text areas.
Usage:
callback = lambda : webbrowser.open("http://www.google.com/")
text = tk.Text(...)
hyperman = tkHyperlinkManager.HyperlinkManager(text)
text.insert(tk.INSERT, "click me", hyperman.add(callback))
From http://effbot.org/zone/tkinter-text-hyperlink.htm
"""
def __init__(self, text, statusfunc=None):
self.text = text
self.statusfunc = statusfunc
self.text.tag_config("hyper", foreground="blue", underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.reset()
def reset(self):
self.links = {}
def add(self, action, tooltip=None):
"""Adds an action to the …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将一段代码移植Python2
到Python3
,此代码中的一个函数返回一个表示表的元组列表,但是这些值是从几个中获取的,tkinter
listboxes
这有两个步骤:
在Python2
该行确实是这样的:
apply(map, [None] + result)
Run Code Online (Sandbox Code Playgroud)
我还没有弄清楚这是如何实际工作或如何移植它,我已经看过文档和我可以看到的关于如何移植到Python3
应用或地图的一切实际上没有实现我想要的东西.
我正在使用Python 3.3
,因为列数不固定,所以首选灵活的答案.
举个例子:
[("c1r1","c1r2","c1r3"),("c2r1","c2r2","c2r3"),("c3r1","c3r2","c3r3")]
Run Code Online (Sandbox Code Playgroud)
需要去:
[("c1r1","c2r1","c3r1"),("c1r2","c2r2","c3r2"),("c1r3","c2r3","c3r3")]
Run Code Online (Sandbox Code Playgroud) python ×5
python-3.x ×4
tkinter ×3
android ×1
android-tv ×1
c++ ×1
combobox ×1
mfc ×1
python-2.7 ×1
ttk ×1