PyQT5: How to open and close 3 windows consecutively - user-interface

I am have a problem in the process of my GUI. I have 3 window which is the main window -> 2nd window -> 3rd window then exit. It works well when clicking the button going to 2nd window, but if I clicked the button to window 3 it crushes.
The whole process crushes without saying any error.
This is my Code for the first window:
from window2 import *
from window3 import *
class Ui_MainWindow(object):
def openWindow2(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2()
self.ui.setupUi2(self.window)
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(637, 309)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow2)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is the code for the 2nd window:
from window3 import *
class Ui_MainWindow2(object):
def openWindow3(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow3()
self.ui.setupUi3(self.window)
MainWindow.hide()
self.window.show()
def setupUi2(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(481, 242)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow3)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setupUi2(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is the code for the 3rd window:
from window1 import *
from window2 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow3(object):
def setupUi3(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(397, 319)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 250, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow3()
ui.setupUi3(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Can someone tell me why the process crushes in the 2nd window when button clicked and why it cannot proceed to the 3rd window?

Try it:
window1.py
from window2 import *
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def openWindow2(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2(self.window) # +++ self.window
self.ui.setupUi2(self.window)
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(637, 309)
MainWindow.setWindowTitle('Window 1')
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton("Button 1", self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow2)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
window2.py
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow2(object):
def __init__(self, window2, *args, **kwargs): # +++
super(Ui_MainWindow2, self).__init__(*args, **kwargs) # +++
self.window2 = window2 # +++
def openWindow3(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow3()
self.ui.setupUi3(self.window)
# MainWindow.hide() # ---
self.window2.hide() # +++
self.window.show()
def setupUi2(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(481, 242)
MainWindow.setWindowTitle('Window 2')
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton("Button 2", self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow3)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setupUi2(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
window3.py
from window1 import *
from window2 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow3(object):
def setupUi3(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(397, 319)
MainWindow.setWindowTitle('Window 3')
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton("Button 3", self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 250, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow3()
ui.setupUi3(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Update
window1.py
from window2 import *
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def openWindow2(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow2(self.window) # +++ self.window
self.ui.setupUi2(self.window)
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(637, 309)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow2)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Window 1"))
self.pushButton.setText(_translate("MainWindow", "Button 1"))
#...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
window2.py
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow2(object):
def __init__(self, window2, *args, **kwargs): # +++
super(Ui_MainWindow2, self).__init__(*args, **kwargs) # +++
self.window2 = window2 # +++
def openWindow3(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow3()
self.ui.setupUi3(self.window)
# MainWindow.hide() # ---
self.window2.hide() # +++
self.window.show()
def setupUi2(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(481, 242)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.openWindow3)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Window 2"))
self.pushButton.setText(_translate("MainWindow", "Button 2"))
#...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow2()
ui.setupUi2(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
window3.py
from window1 import *
from window2 import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow3(object):
def setupUi3(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(397, 319)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 250, 95, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(MainWindow.close) # +++
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Window 3"))
self.pushButton.setText(_translate("MainWindow", "Button 3 (Quit)"))
#...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow3()
ui.setupUi3(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Related

How to add a link annotation with a PDFActionGoto action type to a PDFPage using swift in MACOS

I have been trying to add a link annotation to my PDFPage so that once clicked it will take me to a specific location of the next page. This is the way I am trying but my code is not working:
The cursor does not change to the pointer style.
Once the area is clicked navigation does not occur.
.freetext annotation is working and getting displayed.
I would appreciate any help regarding this. I have been trying this for the last couple of days. I am using latest version of xcode (12 and swift 5.3)
let docDirectoryPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let pdfDocument = PDFDocument()
let page1 = PDFPage()
let page2 = PDFPage()
pdfDocument.insert(page1, at: 0)
pdfDocument.insert(page2, at: 1)
let textAnnotation = PDFAnnotation(bounds: NSRect(x: 200, y: 1000, width: 600, height: 25), forType: .freeText, withProperties: nil)
textAnnotation.contents = "Click me to go to the next page"
let linkAnnotation = PDFAnnotation(bounds: NSRect(x: 200, y: 1000, width: 600, height: 25), forType: .link, withProperties: nil)
linkAnnotation.action = PDFActionGoTo(destination: PDFDestination(page: page2, at: NSPoint(x: 200, y: 1000)))
linkAnnotation.shouldDisplay = true
linkAnnotation.shouldPrint = true
let someText = PDFAnnotation(bounds: NSRect(x: 200, y: 1000, width: 600, height: 25), forType: .freeText, withProperties: nil)
someText.contents = "This is the desitnation of link annotation"
page1.addAnnotation(textAnnotation)
page1.addAnnotation(linkAnnotation)
page2.addAnnotation(someText)
let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.jobDisposition: NSPrintInfo.JobDisposition.preview, NSPrintInfo.AttributeKey.jobSavingURL: docDirectoryPath]
let printInfo = NSPrintInfo(dictionary: printOpts)
printInfo.horizontalPagination = NSPrintInfo.PaginationMode.automatic
printInfo.verticalPagination = NSPrintInfo.PaginationMode.automatic
printInfo.topMargin = 0.0
printInfo.leftMargin = 0.0
printInfo.rightMargin = 0.0
printInfo.bottomMargin = 0.0
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = false
printInfo.paperSize = NSSize(width: 8.5*96, height: 11*96)
printInfo.scalingFactor = 1.0
let printOperation = pdfDocument.printOperation(for: printInfo, scalingMode: .pageScaleNone, autoRotate: false)!
printOperation.showsPrintPanel = true
printOperation.run()
After much trial and error I figured that if the document is run through printOperation linkAnnotation is not retained. So I used save panel to save document to file instead. If you click on the green area of the first page it will take you to the page 2 (Destination Arrived)
import Foundation
import PDFKit
let page1 = PDFPage()
let page2 = PDFPage()
let link = PDFAnnotation(bounds: NSRect(x: 40, y: 750, width: 200, height: 30), forType: .link, withProperties: nil)
let highlight = PDFAnnotation(bounds: NSRect(x: 40, y: 750, width: 200, height: 30), forType: .highlight, withProperties: nil)
highlight.color = .green
highlight.backgroundColor = .red
let text = PDFAnnotation(bounds: NSRect(x: 40, y: 400, width: 200, height: 30), forType: .freeText, withProperties: nil)
text.contents = "Destination Arrived!"
link.action = PDFActionGoTo(destination: PDFDestination(page: page2, at: NSPoint(x: 40, y: 430)))
page1.addAnnotation(highlight)
page1.addAnnotation(link)
page2.addAnnotation(text)
let pdfDoc = PDFDocument()
pdfDoc.insert(page1, at: 0)
pdfDoc.insert(page2, at: 1)
let savePanel = NSSavePanel()
savePanel.title = NSLocalizedString("Save file", comment: "enableFileMenuItems")
savePanel.nameFieldStringValue = "LinkAnnotation.pdf"
savePanel.prompt = NSLocalizedString("Save", comment: "enableFileMenuItems")
savePanel.allowedFileTypes = ["pdf"]
let fileManager = FileManager.default
savePanel.begin() { (result2) -> Void in
if result2 == NSApplication.ModalResponse.OK {
let fileWithExtensionURL = savePanel.url!
if fileManager.fileExists(atPath: fileWithExtensionURL.path) {
} else {
pdfDoc.write(to: fileWithExtensionURL)
}
}
}

Migration from Swift 3 to Swift 4 Navigation Bar broken interface

I have migrated my app from Swift 3.1 to Swift 4.0 (Xcode 8.3.3 to Xcode 9.0) and some part of my interface is broken now. Navigation Bar of Navigation controller is complete mess. Please, look at screenshot:
There are 3 elements:
left Netfnet logo (image)
right Signal strength (image)
right QR Code button
As you can see, two images are too big and not in center and button was deformed (it should be perfect square, all images too).
There is code which generated navigation controller:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
settings()
}
func settings() {
let color = UIColor(red: 81 / 255, green: 155 / 255, blue: 22 / 255, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = color
let logoImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
logoImageView.contentMode = .scaleAspectFit
let logo = UIImage(named: "littleLogoImage")
logoImageView.image = logo
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: logoImageView)
let signalStengthImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
signalStengthImageView.contentMode = .scaleAspectFit
signalStengthImageView.image = UIImage(named: "signalStrength4")
let signalStengthImageItem = UIBarButtonItem(customView: signalStengthImageView)
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "qrCodeButton"), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
let qrCodeButtonItem = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItems = [qrCodeButtonItem, signalStengthImageItem] //
}
}
I can decrease resolution of images directly myself, but I just don't get why everting was fine in Swift 3.1 and in Swift 4.0 is broken.
I will be thankful for any help or advice.
You have to add width and height constraints.
Your barImageView and barButton in CustomNavigationController should be like below :
func barImageView(imageName: String) -> UIBarButtonItem {
let imgView = imageView(imageName: imageName)
let widthConstraint = imgView.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = imgView.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true
return UIBarButtonItem(customView: imgView)
}
func barButton(imageName: String, selector: Selector) -> UIBarButtonItem {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: imageName), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
button.addTarget(self, action: selector, for: .touchUpInside)
let widthConstraint = button.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = button.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true
return UIBarButtonItem(customView: button)
}
Your signalStengthImageView in LogoWithSignalStrength:
signalStengthImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
signalStengthImageView.contentMode = .scaleAspectFit
let widthConstraint = signalStengthImageView.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = signalStengthImageView.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true
In Xcode 9, Navigation bar items are constraints base, Add this:
if #available(iOS 11.0, *) {
logoImageView.widthAnchor.constraint(equalToConstant: 35).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 35).isActive = true
} else {
//set frames
}

Collision detection in Xcode(swift) won't work, I am trying to set up boundaries in which the player cannot cross

I have the SKContactDelegate setup
class GameScene: SKScene, SKPhysicsContactDelegate
{
override func didMoveToView(view: SKView)
{
self.physicsWorld.contactDelegate = self
Here I setup the enum
enum ColliderType:UInt32
{
case Player = 1
case Boundary = 2
}
Here I make the player node.
let playerTexture = SKTexture(imageNamed: "testCircle1.png")
thePlayer = SKSpriteNode(texture: playerTexture)
thePlayer.position = CGPointMake(frame.size.width - 350, frame.size.height/2)
thePlayer.zPosition = 5
thePlayer.size = CGSize(width: 100, height: 100)
thePlayer.name = "playerNode"
thePlayer.physicsBody = SKPhysicsBody(circleOfRadius: playerTexture.size().height/2)
thePlayer.physicsBody!.dynamic = false
thePlayer.physicsBody!.allowsRotation = false
thePlayer.physicsBody!.categoryBitMask = ColliderType.Player.rawValue
thePlayer.physicsBody!.contactTestBitMask = ColliderType.Boundary.rawValue
thePlayer.physicsBody!.collisionBitMask = ColliderType.Boundary.rawValue
addChild(thePlayer)
Then I setup the Boundaries
ground = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(frame.size.width - 150, 10))
ground.position = CGPointMake(frame.size.width/2 - 75, 96)
ground.zPosition = 5
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(ground.size.width, ground.size.height))
ground.physicsBody!.dynamic = false
ground.physicsBody!.allowsRotation = false
ground.physicsBody!.categoryBitMask = ColliderType.Boundary.rawValue
ground.physicsBody!.contactTestBitMask = ColliderType.Player.rawValue
ground.physicsBody!.collisionBitMask = ColliderType.Player.rawValue
addChild(ground)
sky = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(frame.size.width - 150, 10))
sky.position = CGPointMake(frame.size.width/2 - 75, frame.size.height - 96)
sky.zPosition = 5
sky.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(frame.size.width - 150, 10))
sky.physicsBody!.dynamic = false
sky.physicsBody!.allowsRotation = false
sky.physicsBody!.categoryBitMask = ColliderType.Boundary.rawValue
sky.physicsBody!.contactTestBitMask = ColliderType.Player.rawValue
sky.physicsBody!.collisionBitMask = ColliderType.Player.rawValue
addChild(sky)
Then the collision detection
func didBeginContact(contact: SKPhysicsContact)
{
print("Contact")
if contact.bodyA.categoryBitMask == ColliderType.Boundary.rawValue || contact.bodyB.categoryBitMask == ColliderType.Boundary.rawValue
{
print("Contact")
thePlayer.removeAllActions()
}
}
I tried putting the print outside of the if statement to see if it was even detecting collision at all, but it wasn't.
I have looked at many tutorials and followed what they did but it just won't work and I only have a couple more weeks to turn this in.
In the player node
remove this:
thePlayer.physicsBody!.dynamic = false
That is setting the physics body to be a boundary and not a body...

How to add a bottom border to a label as a sublayer in swift?

I am trying to insert a colored line under one of my labels :
let label = UILabel(frame: CGRectMake(0, 0, 70, 40))
label.text = items[index - 1]
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.font = UIFont(name: "Helvetica", size: 15)
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false
var sublayer = label.layer;
sublayer.backgroundColor = UIColor.blueColor().CGColor
sublayer.frame = CGRectMake(0, 0, label.frame.width, 1);
sublayer.borderColor = UIColor.blackColor().CGColor;
sublayer.borderWidth = 1;
self.layer.insertSublayer(sublayer, atIndex: 0)
self.addSubview(label)
How can I set the frame properly so that there is a colored line under my label ?
Ended up using a uiview :
let lineView = UIView(frame: CGRectMake(0,
self.frame.height - 3,
label.frame.width,
3.0))
lineView.backgroundColor = UIColor.blackColor()
self.addSubview(lineView)
Works really well.
Swift 3:
let lineView = UIView(frame: CGRect(x: 0, y: self.frame.height - 3, width: frame.width, height: 3.0))
lineView.backgroundColor = UIColor.black
self.addSubview(lineView)

PyGTK Change Window Opacity By Slider

How would I be able to change the window's opacity from a slider?
I made the code, but I'm stuck on where to go from here.
#!/usr/bin/python
import gtk
class app(gtk.Window):
def __init__(self):
super(app, self).__init__()
self.set_position(gtk.WIN_POS_CENTER)
self.set_title("Opacity Slider Test")
self.set_decorated(True)
self.set_has_frame(False)
self.set_resizable(False)
self.set_default_size(320, 50)
self.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing=4)
hbox = gtk.HBox(spacing=4)
scale = gtk.HScale()
scale.set_range(0, 100)
scale.set_size_request(320, 25)
scale.connect("value-changed", self.opacity_slider)
vbox.add(scale)
self.add(vbox)
self.show_all()
def opacity_slider(self, w):
app()
gtk.main()
#!/usr/bin/python
import gtk
class app(gtk.Window):
def __init__(self):
super(app, self).__init__()
self.set_position(gtk.WIN_POS_CENTER)
self.set_title("Opacity Slider Test")
self.set_decorated(True)
self.set_has_frame(False)
self.set_resizable(False)
self.set_default_size(320, 50)
self.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing=4)
hbox = gtk.HBox(spacing=4)
scale = gtk.HScale()
scale.set_range(0, 100)
scale.set_size_request(320, 25)
scale.set_value(100)
scale.connect("value-changed", self.opacity_slider)
vbox.add(scale)
opacity = gtk.Label()
opacity.set_label("Change Opacity")
vbox.add(opacity)
self.add(vbox)
self.show_all()
def opacity_slider(self, w):
self.set_opacity(w.get_value()/100.0)
app()
gtk.main()

Resources