Python:Tkinter iconbitmap分配仅在模块级别有效

时间:2019-04-20 03:23:32

标签: python tkinter ttk

我正在使用tkinter.ttk窗口,并且正在使用图标设置窗口的iconbitmap。但是root.iconbitmap()在Windows 10上会被忽略。但是有一种避免错误的简便方法:root.tkinter.call('wm', 'iconphoto', root._w, icon)

所以:

from tkinter import *
from tkinter.ttk import *

root=Tk()
root.call('wm', 'iconphoto', root._w, icon)

有效。 但是

def func():
    root=Tk()
    root.call('wm', 'iconphoto', root._w, icon)

起作用。发生错误。有趣的是,该错误与您使用root.iconbitmap()时发生的错误完全相同:

Traceback (most recent call last):
File "E:\test.py", line 95, in <module>
func()
File "E:\test.py", line 36, in func
t.call('wm', 'iconphoto', t._w, icon)
_tkinter.TclError: can't use "pyimagex" as iconphoto: not a photo Image

还有一个有趣的事实:在另一个文件中,我也尝试将其用作功能,它起作用了。在新文件(test.py)中,该文件无效(并且具有相同的功能)。 有谁知道为什么它不起作用以及我可以做些什么来避免错误?预先感谢...

1 个答案:

答案 0 :(得分:1)

如果您已经打开了一个窗口,并想使用自己的图标打开另一个窗口,则应使用Tk()而不是W2 = Toplevel() icon = PhotoImage(file='icon.png') W2.tk.call('wm', 'iconphoto', root._w, icon) 并使用

来更改图标
from tkinter import *
from tkinter.ttk import *

def test():
    root = Toplevel()
    icon = PhotoImage( file='icon.png' )  # path to the icon
    root.tk.call('wm', 'iconphoto', root._w, icon)

r = Tk()

b =  Button(r, text='press', command=test)
b.pack()

mainloop()

示例:

let sphereNode1 = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode1.position = SCNVector3(x: 0, y: 5, z: 3)
scene.rootNode.addChildNode(sphereNode1)

let sphereNode2 = SCNNode(geometry: SCNSphere(radius: 3))
sphereNode2.position = SCNVector3(x: 0, y: 1, z: 0)
scene.rootNode.addChildNode(sphereNode2)

let boxNode = SCNNode(geometry: SCNBox(width: 20, height: 0.1, length: 20, chamferRadius: 0))
boxNode.position = SCNVector3(x: 0, y: -3, z: 0)
scene.rootNode.addChildNode(boxNode)

let light2 = SCNLight()
let lightNodeb2 = SCNNode()
light2.castsShadow = true
light2.automaticallyAdjustsShadowProjection = true
light2.maximumShadowDistance = 40.0
light2.orthographicScale = 1
light2.type = .directional
light2.shadowMapSize = CGSize(width: 2048, height: 2048)

light2.shadowMode = .deferred    // Renders shadows in a postprocessing pass

light2.shadowSampleCount = 128
light2.shadowRadius = 3
light2.shadowBias = 32
light2.zNear = 1
light2.zFar = 1000
lightNodeb2.light = light2

// DIRECTIONAL LIGHT POSITION doesn't matter. Matters only its direction.
// lightNodeb2.position = SCNVector3(x: -1, y: 10, z: 1)

lightNodeb2.rotation = SCNVector4(2, 0, 0, -Float.pi/3)
scene.rootNode.addChildNode(lightNodeb2)