Python Tkinter不能将“ pyimage4”用作iconphoto

时间:2019-12-27 09:49:03

标签: python python-3.x tkinter

在我的程序中,我有一个主窗口,我想在该主窗口初始化之前添加一个加载窗口,但是我无法为此加载窗口添加图标。那里是我的代码:

from tkinter import *
import tkinter.ttk as ttk

root = Tk()
root.withdraw()
root.title('BINGO')

class Loader():
    def __init__(self):
        self.loader = Tk()
        try:
            img = PhotoImage(r'C:\menu-16.png')
            self.loader.tk.call('wm', 'iconphoto', self.loader._w, img)
        except:
            self.loader.attributes('-toolwindow', True)
        self.loader.title('BINGO')
        self.frame = LabelFrame(self.loader)
        self.frame.pack(fill=BOTH, pady=2,padx=2)
        self.label = Label(self.frame, text='Loading...', font=('segoe', 12))
        self.label.pack(side=TOP, pady='4')
        self.progress_bar = ttk.Progressbar(self.frame, mode='determinate', length=464)
        self.progress_bar.pack(side=TOP, fill='x', pady='20')
        self.loader.update()

    def bar(self, value):
        self.progress_bar['value'] += value
        self.loader.update()

    def destroy(self):
        self.loader.destroy()

loader = Loader()
loader.bar(5)

class Main_Window():
    def __init__(self):
        self.mframe = LabelFrame(root)
        pass

loader.bar(100)

root.tk.call('wm', 'iconphoto', root._w, PhotoImage(r'C:\menu-16.png'))

loader.destroy()

Application = Main_Window()
root.mainloop()

在这种情况下,“装载程序”作为工具窗口运行而没有图标,但主“ Main_window”拥有它。该如何解决?

2 个答案:

答案 0 :(得分:1)

我已经确定了解决问题的方法:self.loader = Tk() 编辑 self.loader = Toplevel()

答案 1 :(得分:0)

检查您的代码。进度栏是在class Temp { constructor() { Object.defineProperty(this, "hi", { value: () => { // }, writable: true, configurable: true, enumerable: true }); } } 中定义的,因此您需要将 from queue import Queue import threading import time queue1 = Queue() # this function should finish its work and restart with fresh queue1 def association_helper(): # this program should get the whole data from the queue, add into the list and print it. again it starts with # remaining items in queue (the items which was inserting when this function printings the value) lock = threading.Lock() lock.acquire() items = [] print("Start..") while True: if queue1.qsize()>0: print("Line no 13:", queue1.qsize()) SizeofQueue1 = queue1.qsize() for i in range(SizeofQueue1): items.append(queue1.get()) queue1.task_done() print("Line no 19:", len(items)) print(items) print("Line no 25: done") time.sleep(0.1) lock.release() i = 0 def main(): global i # continuous data coming and adding in queue while True: queue1.put([i]) i += 1 if __name__ == '__main__': # main thread will always run (adding the items in the queue) f_thread = threading.Thread(target=association_helper) f_thread.daemon = True f_thread.start() main() output: Start... Line no 13: 1415 Line no 19: 3794 Line no 25: done Line no 13: 40591 Line no 19: 41856 Line no 25: done Line no 13: 78526 as per expectations, the line no 13 and line no 19 should be same. also, after, line no 25, it should print Start..(because association_helps should finish its execution and run again) 导入为

tkinter.tkk

因为有一个额外的tkinter.tkk,请删除from tkinter import * from tkinter.ttk import * .... img = PhotoImage(r'C:\menu-16.png')) ... root.tk.call('wm', 'iconphoto', root._w, PhotoImage(r'C:\menu-16.png')) 并将)添加到PhotoImage并从)中删除file=

ttk

并且您已将类定义为ttk.Progressbar,并且正在将其调用为img = PhotoImage(file=r'C:\menu-16.png') .... root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file=r'C:\menu-16.png'))

tkinter gui with progress bar