我正在阅读The Hitchhiker的Python指南,还有一个简短的代码片段
foo = 'foo'
bar = 'bar'
foobar = foo + bar # This is good
foo += 'ooo' # This is bad, instead you should do:
foo = ''.join([foo, 'ooo'])
Run Code Online (Sandbox Code Playgroud)
作者指出''.join()并不总是比它快+,所以他并不反对使用+字符串连接.
但为什么foo += 'ooo'不好的做法却被foobar=foo+bar认为是好的?
foo += bar好?foo = foo + 'ooo'好?在此代码片段之前,作者写道:
关于字符串的最后一件事是使用join()并不总是最好的.在您从预定数量的字符串创建新字符串的实例中,使用加法运算符实际上更快,但在上述情况下或者在您添加到现有字符串的情况下,使用join()应该是你喜欢的方法.
我试图像这样设置一个应用程序图标(python3/tkinter):
Interface()
root.title("Quicklist Editor")
root.iconbitmap('@/home/jacob/.icons/qle_icon.ico')
root.resizable(0, 0)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都会收到一条错误消息(闲置),说:
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: error reading bitmap file "/home/jacob/.icons/qle_icon.ico"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我不知道如何设置坐标来裁剪PILs中的图像crop():
from PIL import Image
img = Image.open("Supernatural.xlsxscreenshot.png")
img2 = img.crop((0, 0, 201, 335))
img2.save("img2.jpg")
Run Code Online (Sandbox Code Playgroud)
我试着gThumb去寻找坐标,但如果我选择了一个我想裁剪的区域,我只能找到位置194 336.有人可以帮我吗?
这是我的照片:
我希望收获这个:
这是我的第一个Python 编程代码。我试图通过单击按钮来更改图像。我有 2 个按钮,Start Conversation& Stop Conversation。
当表单加载时没有图像。当单击“开始”按钮时,将显示 ABC 图像。单击“停止”按钮时,应显示 xyz 图像。
我面临的问题是,当我单击“开始”时,会出现相应的图像,但是当我单击“停止”时,会出现新图像,但上一个图像不会消失。两张图像依次显示
我的代码如下
root = Tk()
prompt = StringVar()
root.title("AVATAR")
label = Label(root, fg="dark green")
label.pack()
frame = Frame(root,background='red')
frame.pack()
Run Code Online (Sandbox Code Playgroud)
def Image1():
image = Image.open("C:\Python27\Agent.gif")
photo = ImageTk.PhotoImage(image)
canvas = Canvas(height=200,width=200)
canvas.image = photo # <--- keep reference of your image
canvas.create_image(0,0,anchor='nw',image=photo)
canvas.pack()
def Image2():
image = Image.open("C:\Python27\Hydrangeas.gif")
photo = ImageTk.PhotoImage(image)
canvas = Canvas(height=200,width=200)
canvas.image = photo # <--- keep reference …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 tkinter 在 python 上编写扫雷游戏。我首先使用 10x10 的二维列表创建了一个按钮网格。然后我使用循环创建了每个按钮,这样我就不必手动创建每个按钮并将它们夹在上面。
self.b=[[0 for x in range(1,12)] for y in range(1,12)] #The 2 dimensional list
for self.i in range(1,11):
for self.j in range(1,11):
self.b[self.i][self.j]=tkinter.Button(root,text = (" "),command = lambda: self.delete()) # creating the button
self.b[self.i][self.j].place(x=xp,y=yp) # placing the button
xp+=26 #because the width and height of the button is 26
yp+=26
xp=0
Run Code Online (Sandbox Code Playgroud)
基本上我希望按钮在按下时消失。问题是我不知道如何让程序专门删除我按下的按钮,因为所有按钮都完全相同。创建删除函数时:
def delete(self):
self.b[???][???].destroy()
Run Code Online (Sandbox Code Playgroud)
我不知道如何让程序知道用户按下了哪个按钮,所以它可以删除那个特定的按钮。
问题: 有没有办法让每个按钮都有一些独特的东西,让它与其他按钮区分开来?说给每个按钮分配一个特定的坐标,那么当按钮(2,3)被按下时,数字2和3被传递给删除函数,所以删除函数可以删除按钮(2,3)?
可能是骗子,但找不到匹配项。由于它(看似)简单,因此在我的脑海中感觉像是一个盲点,但无法弄清楚。
问题是Gtk.Entry似乎不听在中定义了什么entry.set_width_chars(5),但我找不到原因。我将代码缩小到一个简约的窗口,以避免产生噪音:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class InterFace:
def __init__(self):
win = Gtk.Window()
maingrid = Gtk.Grid()
win.add(maingrid)
# the attach test
label = Gtk.Label("Test123 ")
maingrid.attach(label, 0, 0, 1, 1)
entry = Gtk.Entry()
entry.set_width_chars(5)
maingrid.attach(entry, 1, 0, 1, 1)
# ok, then the box test
label2 = Gtk.Label("Test456 ")
entry2 = Gtk.Entry()
entry2.set_width_chars(5)
box = Gtk.Box()
maingrid.attach(box, 0, 1, 2, 1)
box.pack_start(label2, False, False, 0)
box.pack_start(entry2, False, False, 0) …Run Code Online (Sandbox Code Playgroud) python ×5
tkinter ×3
python-2.7 ×2
python-3.x ×2
gtk3 ×1
icons ×1
image ×1
launcher ×1
linux ×1
minesweeper ×1
string ×1