闪亮的变量覆盖

时间:2016-10-14 13:09:21

标签: r shiny

我有类似

的东西
step <- 0

observeEvent(input$myInput, {
    print(step)
    if(!is.null(input$myInput) && step == 0 {
        # do something
        step <- 1
        print(step)
     } else if(!is.null(input$myInput) && step == 1) {
       # do something
       print("Been in second condition")
     } else {
       # do something else
     }
})

当我第一次执行我的应用并observeEvent开始时,step符合预期0并且第一个if-condition启动。最后设置step <- 1print()中的step告诉我1observeEvent。但是,当step第二次开始时,0仍为print。那是######################################################### ## customize Title bar ## dotpy.ir ## iraj.jelo@gmail.com ######################################################### import sys from PyQt5 import QtWidgets, QtGui from PyQt5 import QtCore from PyQt5.QtCore import Qt class TitleBar(QtWidgets.QDialog): def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) self.setWindowFlags(Qt.FramelessWindowHint) css = """ QWidget{ Background: #AA00AA; color:white; font:12px bold; font-weight:bold; border-radius: 1px; height: 11px; } QDialog{ Background-image:url('img/titlebar bg.png'); font-size:12px; color: black; } QToolButton{ Background:#AA00AA; font-size:11px; } QToolButton:hover{ Background: #FF00FF; font-size:11px; } """ self.setAutoFillBackground(True) self.setBackgroundRole(QtGui.QPalette.Highlight) self.setStyleSheet(css) self.minimize=QtWidgets.QToolButton(self) self.minimize.setIcon(QtGui.QIcon('img/min.png')) self.maximize=QtWidgets.QToolButton(self) self.maximize.setIcon(QtGui.QIcon('img/max.png')) close=QtWidgets.QToolButton(self) close.setIcon(QtGui.QIcon('img/close.png')) self.minimize.setMinimumHeight(10) close.setMinimumHeight(10) self.maximize.setMinimumHeight(10) label=QtWidgets.QLabel(self) label.setText("Window Title") self.setWindowTitle("Window Title") hbox=QtWidgets.QHBoxLayout(self) hbox.addWidget(label) hbox.addWidget(self.minimize) hbox.addWidget(self.maximize) hbox.addWidget(close) hbox.insertStretch(1,500) hbox.setSpacing(0) self.setSizePolicy(QtWidgets.QSizePolicy.Expanding,QtWidgets.QSizePolicy.Fixed) self.maxNormal=False close.clicked.connect(self.close) self.minimize.clicked.connect(self.showSmall) self.maximize.clicked.connect(self.showMaxRestore) def showSmall(self): box.showMinimized() def showMaxRestore(self): if(self.maxNormal): box.showNormal() self.maxNormal= False self.maximize.setIcon(QtGui.QIcon('img/max.png')) print('1') else: box.showMaximized() self.maxNormal= True print('2') self.maximize.setIcon(QtGui.QIcon('img/max2.png')) def close(self): box.close() def mousePressEvent(self,event): if event.button() == Qt.LeftButton: box.moving = True box.offset = event.pos() def mouseMoveEvent(self,event): if box.moving: box.move(event.globalPos()-box.offset) class Frame(QtWidgets.QFrame): def __init__(self, parent=None): QtWidgets.QFrame.__init__(self, parent) self.m_mouse_down= False self.setFrameShape(QtWidgets.QFrame.StyledPanel) css = """ QFrame{ Background: #D700D7; color:white; font:13px ; font-weight:bold; } """ self.setStyleSheet(css) self.setWindowFlags(Qt.FramelessWindowHint) self.setMouseTracking(True) self.m_titleBar= TitleBar(self) self.m_content= QtWidgets.QWidget(self) vbox=QtWidgets.QVBoxLayout(self) vbox.addWidget(self.m_titleBar) vbox.setContentsMargins(0, 0, 0, 0) vbox.setSpacing(0) layout=QtWidgets.QVBoxLayout() layout.addWidget(self.m_content) layout.setContentsMargins(5, 5, 5, 5) layout.setSpacing(0) vbox.addLayout(layout) # Allows you to access the content area of the frame # where widgets and layouts can be added def contentWidget(self): return self.m_content def titleBar(self): return self.m_titleBar def mousePressEvent(self,event): self.m_old_pos = event.pos() self.m_mouse_down = event.button()== Qt.LeftButton def mouseMoveEvent(self,event): x=event.x() y=event.y() def mouseReleaseEvent(self,event): m_mouse_down=False if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) box = Frame() box.move(60,60) l=QtWidgets.QVBoxLayout(box.contentWidget()) l.setContentsMargins(0, 0, 0, 0) edit=QtWidgets.QLabel("""I would've did anything for you to show you how much I adored you But it's over now, it's too late to save our loveJust promise me you'll think of me Every time you look up in the sky and see a star 'cuz I'm your star.""") l.addWidget(edit) box.show() app.exec_() 陈述所显示的内容。那是为什么?

1 个答案:

答案 0 :(得分:1)

我认为这基本上是一个范围问题。你的处理程序表达式 - 括号中observeEvent之间的东西,被视为它自己的函数。如果查看observeEvent的代码,handlerExpr参数将通过exprToFunction()转换为函数。

在处理程序中,表达式step未定义,因此它从表达式外部获取值0。在表达式step内增加为1,但这只是在handlerExpr的那个实例中,不会在外部将step的原始值更改为1。