【问题标题】:Button doesn't appear at SWT on JythonJython 上的 SWT 上没有出现按钮
【发布时间】:2012-06-28 20:19:55
【问题描述】:

我曾尝试在 Jython 的课堂上设置一个按钮。我正在使用 SWT 组件来创建我的 GUI。按钮不出现,但窗口正常出现。我已经在 J​​ava 中创建了这个类,并且效果很好。

这是我的代码,有什么问题?

from java.lang import Thread as JThread, InterruptedException
from org.eclipse.swt import widgets, layout, SWT 
from org.eclipse.swt.layout import GridLayout, GridData, FillLayout
from org.eclipse.swt.widgets import Composite, Listener#, Event
from org.yakindu.sct.runtime.java.new64 import New64CycleBasedStatemachine
import time

class Cycle(object):
    def __init__(self):

        self.display = widgets.Display()
        self.shell = widgets.Shell(self.display, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL)
        self.shell.setLayout(layout.RowLayout())
        self.shell.setText("Message Window")
        self.shell.setLocation(300, 300)
        self.shell.setSize(300, 150)
        self.thread = JThread()
        self.shell.open()

    def run(self):
        self.statemachine = New64CycleBasedStatemachine()
        self.statemachine.enter()
        while not self.thread.isInterrupted():
            self.statemachine.getInterfaceNewTest().getVarMessage()
            self.statemachine.runCycle()
            try: 
                time.sleep(100)
            except InterruptedException: 
                self.thread.interrupt()


        self.thread.start()

    def show_window(self):
        while not self.shell.isDisposed():
            if not self.display.readAndDispatch():
                self.display.sleep()
        self.display.dispose()

        self.thread.interrupted()

    def create_button(self, statemachine, shell):
        self.buttonComposite = Composite(shell, SWT.NO_SCROLL)
        self.buttonCompositeGridData =  GridData()
        self.buttonCompositeGridData.horizontalAlignment = GridData.FILL
        self.buttonCompositeGridData.grabExcessHorizontalSpace = True
        self.buttonComposite.setLayoutData(self.buttonCompositeGridData)
        self.buttonCompositeLayout = FillLayout()
        self.buttonCompositeLayout.type = SWT.HORIZONTAL
        fillLayout = FillLayout()

        self.buttonComposite.setLayout(fillLayout)
        self.button = widgets.Button(self.buttonComposite, SWT.PUSH);
        self.button.setText("Ok")


foo = Cycle()
foo.run()
foo.create_button()
foo.show_window()

【问题讨论】:

    标签: button swt jython


    【解决方案1】:

    有多个问题:

    1. self.shell.open() 的位置。您在 打开 shell 之后调用 create_button() !请参阅shell.open() 的 javadoc。
    2. 您正在使用RowLayout,然后创建GridData,然后设置FillLayout。我不会告诉你问题所在。运行代码看看。

    片段中的工作示例

    from java.lang import Thread as JThread, InterruptedException
    from org.eclipse.swt import widgets, layout, SWT 
    from org.eclipse.swt.layout import GridLayout, GridData, FillLayout
    from org.eclipse.swt.widgets import Composite, Listener#, Event
    
    import time
    
    class Cycle(object):
        def __init__(self):
    
            self.display = widgets.Display()
            self.shell = widgets.Shell(self.display, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL)
            self.shell.setLayout(layout.RowLayout())
            self.shell.setText("Message Window")
            self.shell.setLocation(300, 300)
            self.shell.setSize(300, 150)
    
        def show_window(self):
            self.shell.open() // I have moved it here. After all the control creation !!
            while not self.shell.isDisposed():
                if not self.display.readAndDispatch():
                    self.display.sleep()
            self.display.dispose()
    
    
        def create_button(self):
            self.buttonComposite = Composite(self.shell, SWT.NO_SCROLL)
            #self.buttonCompositeGridData =  GridData()
            #self.buttonCompositeGridData.horizontalAlignment = GridData.FILL
            #self.buttonCompositeGridData.grabExcessHorizontalSpace = True
            #self.buttonComposite.setLayoutData(self.buttonCompositeGridData)
            self.buttonCompositeLayout = FillLayout()
            self.buttonCompositeLayout.type = SWT.HORIZONTAL
            fillLayout = FillLayout()
    
            self.buttonComposite.setLayout(fillLayout)
            self.button = widgets.Button(self.buttonComposite, SWT.PUSH);
            self.button.setText("Ok")
    
    
    foo = Cycle()
    foo.create_button()
    foo.show_window()
    

    Output-

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 2021-02-23
      • 2016-12-09
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2011-07-20
      • 2011-01-08
      相关资源
      最近更新 更多