我正在编写一些代码来获取物理屏幕的大小,并使用这些尺寸来调整窗口大小:
#!/usr/bin/env python
import gtk
class GettingStarted:
def __init__(self):
window = gtk.Window()
width = gtk.gdk.Screen.get_width()#line1
height = gtk.gdk.Screen.get_height()#line2
window.resize(width,height)#line3
label = gtk.Label("Hello")
window.add(label)
window.connect("destroy", lambda q : gtk.main_quit())
window.show_all()
GettingStarted()
gtk.main()
在代码中注释了line1,line2,line3后,屏幕上会显示一个带"Hello"
的常规窗口。但是,如果代码中包含上述行,则会出于某种原因显示日历!还会抛出错误:
Traceback (most recent call last):
File "gettingstarted.py", line 17, in <module>
GettingStarted()
File "gettingstarted.py", line 8, in __init__
width = gtk.gdk.Screen.get_width()
TypeError: descriptor 'get_width' of 'gtk.gdk.Screen' object needs an argument
在文档中没有提及get_width()
或get_height()
的任何论据。发生了什么事?
答案 0 :(得分:1)
您在两个位置(line1和line2)中使用的是类而不是实例,请在两个位置尝试gtk.gdk.screen_get_default()而不是gtk.gdk.Screen。
#!/usr/bin/env python
import gtk
class GettingStarted:
def __init__(self):
window = gtk.Window()
width = gtk.gdk.screen_get_default().get_width()#line1
height = gtk.gdk.screen_get_default().get_height()#line2
window.resize(width,height)#line3
label = gtk.Label("Hello")
window.add(label)
window.connect("destroy", lambda q : gtk.main_quit())
window.show_all()
GettingStarted()
gtk.main()