【问题标题】:Python Xlib not working when in an imported functionPython Xlib 在导入的函数中不起作用
【发布时间】:2018-03-23 18:19:28
【问题描述】:

我正在尝试使用 python Xlib 库,但遇到了 Xlib 函数在我定义然后导入的函数中似乎不起作用的问题。

例如,如果我以 Python Xlib 自带的示例程序 childwin.py 为例,简单地将其放在一个名为 childtest.py 的文件中的函数中,如下所示:

#Python 2/3 compatibility.
from __future__ import print_function

import sys
import os

# Change path so we find Xlib
def xlib_test():
    print('Running xlib_test()')

    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from Xlib import X, display, Xutil

    # Application window
    class Window(object):
        def __init__(self, display):
            self.d = display

            # Find which screen to open the window on
            self.screen = self.d.screen()

            # background pattern
            bgsize = 20

            bgpm = self.screen.root.create_pixmap(
                bgsize,
                bgsize,
                self.screen.root_depth
                )

            bggc = self.screen.root.create_gc(
                foreground=self.screen.black_pixel,
                background=self.screen.black_pixel
                )

            bgpm.fill_rectangle(bggc, 0, 0, bgsize, bgsize)

            bggc.change(foreground=self.screen.white_pixel)

            bgpm.arc(bggc, -bgsize // 2, 0, bgsize, bgsize, 0, 360 * 64)
            bgpm.arc(bggc, bgsize // 2, 0, bgsize, bgsize, 0, 360 * 64)
            bgpm.arc(bggc, 0, -bgsize // 2, bgsize, bgsize, 0, 360 * 64)
            bgpm.arc(bggc, 0, bgsize // 2, bgsize, bgsize, 0, 360 * 64)

           # Actual window
            self.window = self.screen.root.create_window(
                100, 100, 400, 300, 0,
                self.screen.root_depth,
                X.InputOutput,
                X.CopyFromParent,

                # special attribute values
                background_pixmap=bgpm,
                event_mask=(
                    X.StructureNotifyMask |
                    X.ButtonReleaseMask
                    ),
                colormap=X.CopyFromParent
                )

            # Set some WM info

            self.WM_DELETE_WINDOW = self.d.intern_atom('WM_DELETE_WINDOW')
            self.WM_PROTOCOLS = self.d.intern_atom('WM_PROTOCOLS')

            self.window.set_wm_name('Xlib example: childwin.py')
            self.window.set_wm_icon_name('childwin.py')
            self.window.set_wm_class('childwin', 'XlibExample')

            self.window.set_wm_protocols([self.WM_DELETE_WINDOW])
            self.window.set_wm_hints(
                flags=Xutil.StateHint,
                initial_state=Xutil.NormalState
                )

            self.window.set_wm_normal_hints(
                flags=(Xutil.PPosition | Xutil.PSize | Xutil.PMinSize),
                min_width=50,
                min_height=50
                )

            # Map the window, making it visible
            self.window.map()

            # Child window
            (self.childWidth, self.childHeight) = (20, 20)
            self.childWindow = self.window.create_window(
                20, 20, self.childWidth, self.childHeight, 0,
                self.screen.root_depth,
                X.CopyFromParent,
                X.CopyFromParent,

                # special attribute values
                background_pixel=self.screen.white_pixel,
                colormap=X.CopyFromParent,
                )
            self.childWindow.map()


        # Main loop, handling events
        def loop(self):
            current = None
            while 1:
                e = self.d.next_event()

                # Window has been destroyed, quit
                if e.type == X.DestroyNotify:
                    sys.exit(0)

                # Button released, add or subtract
                elif e.type == X.ButtonRelease:
                    if e.detail == 1:
                        print("Moving child window.")
                        self.childWindow.configure(
                            x=e.event_x - self.childWidth // 2,
                            y=e.event_y - self.childHeight // 2
                            )
                        self.d.flush()

                # Somebody wants to tell us something
                elif e.type == X.ClientMessage:
                    if e.client_type == self.WM_PROTOCOLS:
                        fmt, data = e.data
                        if fmt == 32 and data[0] == self.WM_DELETE_WINDOW:
                            sys.exit(0)


    if __name__ == '__main__':
        Window(display.Display()).loop()

xlib_test()

由于最后一行 xlib_test(),如果我通过键入来运行此文件

python childtest.py

在终端中,程序运行并出现一个窗口。但是,如果我打开一个 python 解释器,导入函数然后运行它,

from childtest import xlib_test
xlib_test()

没有显示窗口,因此 Xlib 函数似乎不起作用。有谁能够解释这种行为以及我如何纠正我的设置来解决它?我的目标是在一个单独的文件中编写我的 Xlib 函数,然后在我的主代码中导入这些函数。

【问题讨论】:

    标签: python xlib


    【解决方案1】:

    很正常,是因为这行:

    if __name__ == '__main__':
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2017-05-30
      • 1970-01-01
      • 2014-11-09
      • 2018-01-08
      • 2021-11-10
      相关资源
      最近更新 更多