【发布时间】:2017-08-17 13:21:54
【问题描述】:
我正在尝试让 .gif 动画在带有按钮的图片旁边工作。但我似乎遇到了问题,我正在导入这些模块 “导入 Tkinter”和“从 PIL 导入 Image、ImageTk、ImageSequence” 但是,一旦我做出“import Tkinter”---“from Tkinter import *” 它说 Tkinter 没有定义,我已经搜索过......并且搜索过......而且我无法为我的死找到解决方案。 我必须使用“from Tkinter import *”,因为我不知道在哪里可以找到“*”的替代词我还需要使用“Label”、“bg”和“relief” 这是我的代码:
import Tkinter
from PIL import Image, ImageTk, ImageSequence
class App:
def __init__(self, parent):
self.parent = parent
self.canvas = Tkinter.Canvas(parent, width = 400, height = 500)
self.canvas.pack()
self.sequence = [ImageTk.PhotoImage(img)
for img in ImageSequence.Iterator(
Image.open(
r'C:\Users\Damian\Pictures\Gif folder\Originals\Bunnychan.gif'))]
self.image = self.canvas.create_image(200,200, image=self.sequence[0])
self.animating = True
self.animate(0)
def animate(self, counter):
self.canvas.itemconfig(self.image, image=self.sequence[counter])
if not self.animating:
return
self.parent.after(33, lambda: self.animate((counter+2) % len(self.sequence)))
root = Tkinter.Tk()
root.title('App')
app = App(root)
root.mainloop()
我还有另一部分要与此代码合并:
import webbrowser
from Tkinter import *
from PIL import ImageTk,Image
Url1 = 'https://www.nedbank.co.za'
Url2 = 'https://www.facebook.com'
def openUrl1():
webbrowser.open(Url1, 2)
def openUrl2():
webbrowser.open(Url2, 2)
root = Tk()
root.title('App')
root.minsize(width = 400, height = 400)
root.maxsize(width = 400, height = 400)
image = Image.open("C:\\Users\\Damian\\Pictures\\Lores.png")
photo = ImageTk.PhotoImage(image)
label = Label(image = photo)
label.image = photo
label.place(x = 0, y = 0)
color1 = 'white'
button1 = Button(
text = color1,
bg = color1,
relief = "raised",
width = 220,
height = 85,
command = openUrl1
)
Original = Image.open("C:\\Users\\Damian\\Pictures\\NedbankLogoNew.png")
im_pm = ImageTk.PhotoImage(Original)
button1.config(image = im_pm)
button1.place(x = 0, y = 0)
root.mainloop()
【问题讨论】:
-
您为什么要将
import Tkinter更改为from Tkinter import *?第一种是首选方式。为了得到你需要的东西,你可以写from Tkinter import Label, bg, relief这样的东西。 -
我导入了“标签”,现在似乎导入了我的图片,谢谢!只需要看看 bg 和其他人是否工作......因为我到目前为止无法导入 bg 和救济......
-
非常感谢!一切正常。
-
太好了,很高兴听到。通过查看系统上的
PythonXX/Lib/lib-tk/Tkconstants.py文件,您可以看到Tkinter定义的符号常量。您会发现大多数只是联系人名称的小写版本:即TOP='top'。另请注意,它们都是UPPERCASE名称。
标签: python python-2.7 tkinter