2017-08-17 64 views
0

我想获得一个.gif动画工作旁边的按钮上的图片。但我似乎有问题,我导入这些模块 “导入Tkinter”和“从PIL导入图像,ImageTk,ImageSequence” 但是,只要我“导入Tkinter”---“从Tkinter导入* “ 它说Tkinter没有定义,我搜查了..并搜查....我不能为我的死亡找到解决办法。 我不得不使用“从Tkinter导入*”,因为我不知道在哪里可以找到“*”的子项的特征,我还需要使用“标签”,“bg”和“relief” 这里是我的代码:Tkinter和PIL错误

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() 
+0

你为什么试图将'import Tkinter'从Tkinter import *'更改为''?第一种是首选方式。为了获得您需要的物品,您可以写一些类似'从Tkinter import Label,bg,relief'。 – martineau

+0

我导入了“标签”,似乎现在导入我的照片,谢谢!只需要看看bg和其他人的工作......因为我目前无法导入BG和救济...... – Damian

+0

谢谢一堆!一切正常。 – Damian

回答

0

推荐的方式导入Tkinter的是这样的:

import Tkinter as tk 

一旦你这样做,你会像平常一样使用tkinter,但是你需要在所有的小部件和常量上加上tk.。当然,你可以做import Tkinter,但这意味着你必须在Tkinter.之前加上前缀,这有点过于罗嗦IMO。

例如:

import Tkinter as tk 

root = tk.Tk() 
label = tk.Label(root, relief=tk.RAISED) 
... 

我个人不建议使用常量。你可以使用一个字符串(例如:relief="raised",sticky="nsew"等),它给出了完全相同的结果。