我在玩弄这个Python库:
有一个名为PopUpDialog的对象,它的基类是Frame类型
帧对象的字段之一是可以滚动=True
当我实例化弹出对话框时,我希望该字段为假,这样就不会出现滚动条
我试图创建一个名为so的新类
class PopUpDialogNoScroll(PopUpDialog):
super(PopUpdDialogNoScroll, self).__init__(screen,...
及
class PopUpDialogNoScroll(Frame):
super(PopUpdDialogNoScroll, self).__init__(screen,...
但是,我所有的尝试都导致了一个错误,如“给定7个参数,预期2个”
有人能告诉我如何派生类并设置父类和/或祖父母类的字段吗
谢谢,祝你身体健康
编辑 多亏了awarrier 99,我走了这么远,但是滚动条仍然可见。这里我有一些演示代码
from asciimatics.widgets import Frame, ListBox, Layout, Divider, Text, TextBox, Widget, Button, Label, DropdownList, PopUpDialog, CheckBox, RadioButtons, PopupMenu
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError, NextScene, StopApplication, InvalidFields
class PopUpDialogNoScroll(PopUpDialog):
def __init__(self, *args, **kwargs):
super(PopUpDialogNoScroll, self).__init__(*args, **kwargs)
self.can_scroll = False
self.has_border = False
class DemoView(Frame):
def __init__(self, screen):
super(DemoView, self).__init__(screen,
11,
48,
data=None,
on_load=None,
has_border=True,
hover_focus=True,
name=None,
title="Welcome",
x=None,
y=None,
has_shadow=False,
reduce_cpu=False,
is_modal=False,
can_scroll=False)
body = Layout([1], fill_frame=True)
self.add_layout(body)
button = Button("Click me", self.clicked, label=None)
body.add_widget(button, 0)
def clicked(self):
self._scene.add_effect(
PopUpDialogNoScroll(self.screen, "1..\n2..\n3..\n4..\n5.. ", ["OK"], on_close=self.done, has_shadow=False, theme='info'))
def done(self):
pass
last_scene = None
def demo(screen, scene):
scenes = [
Scene([DemoView(screen)], duration=-1, clear=True, name="demo"),
]
screen.play(scenes, stop_on_resize=False, unhandled_input=None, start_scene=scene, repeat=True, allow_int=True)
Screen.wrapper(demo, catch_interrupt=True, arguments=[last_scene])
首先,对super的任何调用都应该在类自己的
__init__
函数的第一行中进行。因此,您的类应该有自己的__init__
函数。然后,要将所有想要的参数传递给原始类,可以提供两个参数来合并继承类将具有的所有参数:*args
和**kwargs
。然后,可以在__init__
函数中将can_scroll字段显式设置为False,如下所示:通过使用
*args
和**kwargs
,可以传入原始PopUpDialog
类所期望的任何参数,而无需显式重写它们,并且可以将它们提供给super
类编辑: 将代码迁移到Python3时,可以将
super
调用更改为以下内容,这是Python3中的新语法:super().__init(*args, **kwargs)
。请注意,唯一的区别是不必显式指定类名和self
引用编辑2: 查看here提供的源代码,我能够通过复制他们在
PopUpDialog
类中使用的任何代码,然后从源代码更改这一行来创建PopUpDialogNoScroll
类:super(PopUpDialog, self).__init__(screen, height, width, self._data, has_shadow=has_shadow, is_modal=True)
致:
super(PopUpDialogNoScroll, self).__init__(screen, height, width, self._data, has_shadow=has_shadow, is_modal=True, can_scroll=False)
虽然不是最优雅的解决方案,但它确实有效
相关问题 更多 >
编程相关推荐