这是一个Python 101类型的问题,但是当我尝试使用似乎将我的字符串输入转换为字节的包时,我感到困惑了一段时间.
正如您将在下面看到的,我找到了自己的答案,但我觉得这里值得记录,因为我花了很长时间才发现了正在发生的事情.它似乎是Python 3的通用,所以我没有提到我正在玩的原始包; 它似乎不是一个错误(只是特定的包有一个.tostring()方法明显没有产生我理解为字符串...)
我的测试程序是这样的:
import mangler # spoof package
stringThing = """
<Doc>
<Greeting>Hello World</Greeting>
<Greeting>??</Greeting>
</Doc>
"""
# print out the input
print('This is the string input:')
print(stringThing)
# now make the string into bytes
bytesThing = mangler.tostring(stringThing) # pseudo-code again
# now print it out
print('\nThis is the bytes output:')
print(bytesThing)
Run Code Online (Sandbox Code Playgroud)
此代码的输出给出了:
This is the string input:
<Doc>
<Greeting>Hello World</Greeting>
<Greeting>??</Greeting>
</Doc>
This is the bytes output:
b'\n<Doc>\n <Greeting>Hello World</Greeting>\n <Greeting>\xe4\xbd\xa0\xe5\xa5\xbd</Greeting>\n</Doc>\n'
Run Code Online (Sandbox Code Playgroud)
因此,需要能够在字节和字符串之间进行转换,以避免最终将非ascii字符转换为gobbledegook.
我最近下载并安装了Anjuta作为Python开发的工具.我正在使用Ubuntu 12.04平台上的Python 3.2.3进行开发.
在Gnome开发中心网站上的"创建新项目"建议之后,我开始按照说明进行操作,但很快意识到该项目使用的是Python 2.7(系统默认).
我在"编辑首选项"下找到了一个Python选项,但是将路径更改为指向我所需的Python版本似乎没有达到预期的效果.现在,每次打开Anjuta时,都会显示一条错误配置路径的消息.
我在网上找不到任何关于此的信息.
有没有人有为Python3设置Anjuta的经验?请帮忙!
问题的核心是,我在下面的代码片段中做错了什么?
from tkinter import *
from tkinter.ttk import *
root = Tk()
myButton = Button(root)
myImage = PhotoImage(myButton, file='myPicture.gif')
myButton.image = myImage
myButton.configure(image=myImage)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我从idle3得到的错误信息如下:
>>>
Traceback (most recent call last):
File "/home/bob/Documents/Python/tkImageTest.py", line 9, in <module>
myButton.configure(image=myImage)
File "/usr/lib/python3.2/tkinter/__init__.py", line 1196, in configure
return self._configure('configure', cnf, kw)
File "/usr/lib/python3.2/tkinter/__init__.py", line 1187, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TypeError: __str__ returned non-string (type Button)
>>>
Run Code Online (Sandbox Code Playgroud)
这个错误信息让我难过,我根本不明白它想说的是什么.有任何想法吗?
我也很感激改变的建议......
我最近的头疼是使用 GTK3 在 Python3 中构建一个愚蠢的小应用程序,按钮上的颜色不是雾灰色。最近几天我一直在谷歌上搜索如何做到这一点,到目前为止我尝试过的一切都失败了。不仅失败了,而且默默地失败了,没有错误消息给我任何关于发生了什么的线索。
这是我的测试应用程序:
from gi.repository import Gtk, Gdk
class ButtonWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Button Test")
self.set_border_width(10)
hbox = Gtk.Box(spacing=10)
self.add(hbox)
hbox.set_homogeneous(False)
# make the button
button = Gtk.Button('Test Button')
hbox.pack_start(button, True, True, 0)
# try to change its colour ....
# button.modify_base(Gtk.StateType.NORMAL, Gdk.color_parse('green'))
# button.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0, 1, 0, 1))
# button.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0x00ff00))
# button.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse("green"))
# button.modify_bg(Gtk.StateType.ACTIVE, Gdk.color_parse("green"))
# button.modify_bg(Gtk.StateType.SELECTED, Gdk.color_parse("green"))
# attempt to change the style ....
# style = button.get_style().copy()
# style.bg[Gtk.StateType.NORMAL] = Gdk.color_parse('green') …Run Code Online (Sandbox Code Playgroud) 虽然我已经找到了这个问题的部分和间接答案(参见,例如,这个链接),但我在这里发布这个,因为把拼图的部分拼凑起来花了我一点时间,我想其他人可能会找到我的努力使用.
那么,当父窗口调整大小时,如何在GTK +中的按钮上实现图像的无缝调整?
我最初将它实现为一个包含在列表中的包装类,但我对我需要提供的operator()方法的数量感到恼火,所以我只是简单地继承了list.这是我的测试代码:
class CleverList(list):
def __add__(self, other):
copy = self[:]
for i in range(len(self)):
copy[i] += other[i]
return copy
def __sub__(self, other):
copy = self[:]
for i in range(len(self)):
copy[i] -= other[i]
return copy
def __iadd__(self, other):
for i in range(len(self)):
self[i] += other[i]
return self
def __isub__(self, other):
for i in range(len(self)):
self[i] -= other[i]
return self
a = CleverList([0, 1])
b = CleverList([3, 4])
print('CleverList does vector arith: a, b, a+b, a-b = ', a, b, a+b, …Run Code Online (Sandbox Code Playgroud)