Best Python code snippet using molotov_python
MyWindowController.py
Source:MyWindowController.py
1import objc2import Cocoa3# date/time element popup selections:4kNSHourMinuteDatePickerElementFlag = 05kNSHourMinuteSecondDatePickerElementFlag = 16kNSTimeZoneDatePickerElementFlag = 27kNSYearMonthDatePickerElementFlag = 08kNSYearMonthDayDatePickerElementFlag = 19kNSEraDatePickerElementFlag = 210kSingleDateMode = 011kRangeDateMode = 112class MyWindowController (Cocoa.NSWindowController):13 datePickerControl = objc.ivar()14 shrinkGrowFactor = objc.ivar.int()15 outerBox = objc.IBOutlet()16 datePickerPlaceHolder = objc.IBOutlet()17 dateResult1 = objc.IBOutlet()18 dateResult2 = objc.IBOutlet()19 dateResult3 = objc.IBOutlet()20 dateResult4 = objc.IBOutlet()21 dateResult5 = objc.IBOutlet()22 # appearance23 pickerStylePopup = objc.IBOutlet()24 drawsBackgroundCheck = objc.IBOutlet()25 bezeledCheck = objc.IBOutlet()26 borderedCheck = objc.IBOutlet()27 backColorWell = objc.IBOutlet()28 textColorWell = objc.IBOutlet()29 fontSizePopup = objc.IBOutlet()30 # date and time31 dateElementChecks = objc.IBOutlet()32 timeElementChecks = objc.IBOutlet()33 overrideDateCheck = objc.IBOutlet()34 overrideDate = objc.IBOutlet()35 # date range36 datePickerModeRadios = objc.IBOutlet()37 secondsRangeEdit = objc.IBOutlet()38 secondsRangeEditLabel = objc.IBOutlet()39 minDatePicker = objc.IBOutlet()40 maxDatePicker = objc.IBOutlet()41 setMinDateButton = objc.IBOutlet()42 setMaxDateButton = objc.IBOutlet()43 clearMinDateButton = objc.IBOutlet()44 clearMaxDateButton = objc.IBOutlet()45 def awakeFromNib(self):46 # based our date formatter on CFDateFormatter: allows more configurability and better localization47 Cocoa.NSDateFormatter.setDefaultFormatterBehavior_(Cocoa.NSDateFormatterBehavior10_4)48 self.setupDatePickerControl_(Cocoa.NSClockAndCalendarDatePickerStyle)49 # setup the initial Cocoa.NSDatePickerElementFlags since we are using picker style:50 # Cocoa.NSClockAndCalendarDatePickerStyle51 flags = (52 Cocoa.NSYearMonthDatePickerElementFlag53 | Cocoa.NSYearMonthDayDatePickerElementFlag54 | Cocoa.NSEraDatePickerElementFlag55 | Cocoa.NSHourMinuteDatePickerElementFlag56 | Cocoa.NSHourMinuteSecondDatePickerElementFlag57 | Cocoa.NSTimeZoneDatePickerElementFlag58 )59 self.datePickerControl.setDatePickerElements_(flags)60 self.datePickerModeRadios.cellWithTag_(1).setEnabled_(False) # not currently implemened in 10.4.x and earlier61 self.minDatePicker.setDateValue_(Cocoa.NSDate.date())62 self.maxDatePicker.setDateValue_(Cocoa.NSDate.distantFuture())63 self.updateControls() # force update of all UI elements and the picker itself64 def applicationShouldTerminateAfterLastWindowClosed_(self, sender):65 return True66 def setupDatePickerControl_(self, pickerStyle):67 # we need to re-create the picker control (due to a resize bug when switching between styles)68 if self.datePickerControl is not None: # hide and release the previous date picker, if any69 self.datePickerControl.setHidden_(True)70 self.datePickerControl.setNeedsDisplay_(True)71 self.datePickerControl = None72 frame = self.datePickerPlaceHolder.frame()73 self.shrinkGrowFactor = frame.size.height - 3074 # create the date picker control if not created already75 if self.datePickerControl is None:76 self.datePickerControl = Cocoa.NSDatePicker.alloc().initWithFrame_(frame)77 self.datePickerControl.setFrameOrigin_(Cocoa.NSMakePoint(1,0)) # nudge the control placement a little78 self.datePickerControl.setDatePickerStyle_(pickerStyle) # set our desired picker style79 self.datePickerPlaceHolder.addSubview_(self.datePickerControl) # embed into our placeholder80 self.datePickerControl.setDrawsBackground_(True)81 self.datePickerControl.setBezeled_(True)82 self.datePickerControl.setBordered_(False)83 self.datePickerControl.setEnabled_(True)84 self.datePickerControl.setTextColor_(self.textColorWell.color())85 self.datePickerControl.setBackgroundColor_(self.backColorWell.color())86 # always set the date/time to TODAY87 # note that our delete override might block this...88 self.datePickerControl.setDateValue_(Cocoa.NSDate.date())89 self.datePickerControl.setNeedsDisplay_(True)90 self.updateControls() # force update of all UI elements and the picker itself91 # synch the picker style popup with the new style change92 self.pickerStylePopup.selectItemWithTag_(pickerStyle)93 # we want to be the cell's delegate to catch date validation94 self.datePickerControl.setDelegate_(self)95 # or we can set us as the delegate to its cell like so:96 # self.datePickerControl.cell().setDelegate_(self)97 # we want to respond to date/time changes98 self.datePickerControl.setAction_('datePickerAction:')99 def updateDateResult(self):100 theDate = self.datePickerControl.dateValue()101 if theDate is not None:102 formatter = Cocoa.NSDateFormatter.alloc().init()103 # some examples:104 # formatter.setDateStyle_(Cocoa.NSDateFormatterNoStyle) # <no date displayed>105 # formatter.setDateStyle_(Cocoa.NSDateFormatterMediumStyle) # Jan 24, 1984106 # formatter.setDateStyle_(Cocoa.NSDateFormatterShortStyle) # 1/24/84107 # formatter.setDateStyle_(Cocoa.NSDateFormatterLongStyle) # January 24, 1984108 # formatter.setDateStyle_(Cocoa.NSDateFormatterFullStyle) # Tuesday, January 24, 1984109 #110 # formatter.setTimeStyle_(Cocoa.NSDateFormatterNoStyle) # <no time displayed>111 # formatter.setTimeStyle_(Cocoa.NSDateFormatterShortStyle) # 2:44 PM112 # formatter.setTimeStyle_(Cocoa.NSDateFormatterMediumStyle) # 2:44:55 PM113 # formatter.setTimeStyle_(Cocoa.NSDateFormatterLongStyle) # 2:44:55 PM PDT114 # formatter.setTimeStyle_(Cocoa.NSDateFormatterFullStyle) # 2:44:55 PM PDT115 #116 formatter.setDateStyle_(Cocoa.NSDateFormatterShortStyle)117 formatter.setTimeStyle_(Cocoa.NSDateFormatterNoStyle)118 formattedDateString = formatter.stringFromDate_(theDate)119 self.dateResult1.setStringValue_(formattedDateString)120 formatter.setDateStyle_(Cocoa.NSDateFormatterShortStyle)121 formatter.setTimeStyle_(Cocoa.NSDateFormatterShortStyle)122 formattedDateString = formatter.stringFromDate_(theDate)123 self.dateResult2.setStringValue_(formattedDateString)124 formatter.setDateStyle_(Cocoa.NSDateFormatterMediumStyle)125 formatter.setTimeStyle_(Cocoa.NSDateFormatterShortStyle)126 formattedDateString = formatter.stringFromDate_(theDate)127 self.dateResult3.setStringValue_(formattedDateString)128 formatter.setDateStyle_(Cocoa.NSDateFormatterLongStyle)129 formatter.setTimeStyle_(Cocoa.NSDateFormatterShortStyle)130 formattedDateString = formatter.stringFromDate_(theDate)131 self.dateResult4.setStringValue_(formattedDateString)132 formatter.setDateStyle_(Cocoa.NSDateFormatterFullStyle)133 formatter.setTimeStyle_(Cocoa.NSDateFormatterFullStyle)134 formattedDateString = formatter.stringFromDate_(theDate)135 self.dateResult5.setStringValue_(formattedDateString)136 def updateControls(self):137 self.datePickerControl.setNeedsDisplay_(True) # force it to update138 self.updateDatePickerMode()139 self.updateDateTimeElementFlags()140 self.updateDateResult()141 @objc.IBAction142 def setPickerStyle_(self, sender):143 tag = sender.selectedCell().tag()144 windowFrame = self.window().frame()145 boxFrame = self.outerBox.frame()146 self.datePickerControl.setHidden_(True)147 if tag == Cocoa.NSClockAndCalendarDatePickerStyle:148 # for this picker style, we need to grow the window to make room149 windowFrame.size.height += self.shrinkGrowFactor;150 windowFrame.origin.y -= self.shrinkGrowFactor;151 boxFrame.size.height += self.shrinkGrowFactor;152 self.outerBox.setFrame_(boxFrame)153 self.window().setFrame_display_animate_(windowFrame, True, True)154 # set our desired picker style155 self.datePickerControl.setDatePickerStyle_(Cocoa.NSClockAndCalendarDatePickerStyle)156 # shows these last157 self.dateResult1.setHidden_(False)158 self.dateResult2.setHidden_(False)159 self.dateResult3.setHidden_(False)160 self.dateResult4.setHidden_(False)161 else:162 currentPickerStyle = self.datePickerControl.datePickerStyle()163 # shrink the window only if the current style is "clock and calendar"164 if currentPickerStyle == Cocoa.NSClockAndCalendarDatePickerStyle:165 # hide these first166 self.dateResult1.setHidden_(True)167 self.dateResult2.setHidden_(True)168 self.dateResult3.setHidden_(True)169 self.dateResult4.setHidden_(True)170 windowFrame.size.height -= self.shrinkGrowFactor171 windowFrame.origin.y += self.shrinkGrowFactor172 boxFrame.size.height -= self.shrinkGrowFactor173 self.outerBox.setFrame_(boxFrame)174 self.window().setFrame_display_animate_(windowFrame, True, True)175 self.setupDatePickerControl_(tag) # set our desired picker style176 self.datePickerControl.setHidden_(False)177 self.updateControls() # force update of all UI elements and the picker itself178 @objc.IBAction179 def setFontSize_(self, sender):180 tag = sender.selectedCell().tag()181 if tag == Cocoa.NSMiniControlSize:182 self.datePickerControl.cell().setControlSize_(Cocoa.NSMiniControlSize)183 self.datePickerControl.cell().setFont_(Cocoa.NSFont.systemFontOfSize_(9.0))184 elif tag == Cocoa.NSSmallControlSize:185 self.datePickerControl.cell().setControlSize_(Cocoa.NSSmallControlSize)186 self.datePickerControl.cell().setFont_(Cocoa.NSFont.systemFontOfSize_(11.0))187 elif tag == Cocoa.NSRegularControlSize:188 self.datePickerControl.cell().setControlSize_(Cocoa.NSRegularControlSize)189 self.datePickerControl.cell().setFont_(Cocoa.NSFont.systemFontOfSize_(13.0))190 @objc.IBAction191 def datePickerAction_(self, sender):192 self.updateDateResult()193 @objc.IBAction194 def dateOverrideAction_(self, sender):195 checked = sender.selectedCell().state()196 if checked:197 self.datePickerControl.setDelegate_(self)198 else:199 self.datePickerControl.setDelegate_(None)200 self.datePickerControl.setDateValue_(Cocoa.NSDate.date()) # force the delete "datePickerCell" to be called201 def datePickerCell_validateProposedDateValue_timeInterval_(self, aDatePickerCell, proposedDateValue, proposedTimeInterval):202 controller = aDatePickerCell.delegate()203 if controller is self and aDatePickerCell is self.datePickerControl.cell():204 # override the date and time?205 if self.overrideDateCheck.cell().state():206 # override the date using the user specified date207 return (self.overrideDate.dateValue(), proposedTimeInterval)208 return (proposedDateValue, proposedTimeInterval)209 @objc.IBAction210 def setDrawsBackground_(self, sender):211 self.datePickerControl.setDrawsBackground_(sender.state())212 @objc.IBAction213 def setBackgroundColor_(self, sender):214 newColor = sender.color()215 self.datePickerControl.setBackgroundColor_(newColor)216 @objc.IBAction217 def setTextColor_(self, sender):218 newColor = sender.color()219 self.datePickerControl.setTextColor_(newColor)220 @objc.IBAction221 def setBezeled_(self, sender):222 self.datePickerControl.setBezeled_(sender.state())223 @objc.IBAction224 def setBordered_(self, sender):225 self.datePickerControl.setBordered_(sender.state())226 @objc.IBAction227 def setDateElementFlags_(self, sender):228 tag = sender.selectedCell().tag()229 flags = self.datePickerControl.datePickerElements()230 checked = sender.selectedCell().state()231 if tag == kNSYearMonthDatePickerElementFlag:232 if checked:233 flags |= Cocoa.NSYearMonthDatePickerElementFlag234 else:235 flags ^= Cocoa.NSYearMonthDatePickerElementFlag236 elif tag == kNSYearMonthDayDatePickerElementFlag:237 if checked:238 flags |= Cocoa.NSYearMonthDayDatePickerElementFlag239 else:240 flags ^= Cocoa.NSYearMonthDayDatePickerElementFlag241 elif tag == kNSEraDatePickerElementFlag:242 if checked:243 flags |= Cocoa.NSEraDatePickerElementFlag244 else:245 flags ^= Cocoa.NSEraDatePickerElementFlag246 self.datePickerControl.setDatePickerElements_(flags)247 self.updateControls() # force update of all UI elements and the picker itself248 @objc.IBAction249 def setTimeElementFlags_(self, sender):250 tag = sender.selectedCell().tag()251 flags = self.datePickerControl.datePickerElements()252 checked = sender.selectedCell().state()253 if tag == kNSHourMinuteDatePickerElementFlag:254 if checked:255 flags |= Cocoa.NSHourMinuteDatePickerElementFlag256 else:257 flags ^= Cocoa.NSHourMinuteDatePickerElementFlag258 elif tag == kNSHourMinuteSecondDatePickerElementFlag:259 if checked:260 flags |= Cocoa.NSHourMinuteSecondDatePickerElementFlag261 else:262 flags ^= Cocoa.NSHourMinuteSecondDatePickerElementFlag263 elif tag == kNSTimeZoneDatePickerElementFlag:264 if checked:265 flags |= Cocoa.NSTimeZoneDatePickerElementFlag266 else:267 flags ^= Cocoa.NSTimeZoneDatePickerElementFlag268 self.datePickerControl.setDatePickerElements_(flags)269 self.updateControls() # force update of all UI elements and the picker itself270 def updateDateTimeElementFlags(self):271 elementFlags = self.datePickerControl.datePickerElements()272 # time elements273 if (elementFlags & Cocoa.NSHourMinuteDatePickerElementFlag) != 0:274 self.timeElementChecks.selectCellWithTag_(0)275 if (elementFlags & Cocoa.NSHourMinuteSecondDatePickerElementFlag) != 0:276 self.timeElementChecks.selectCellWithTag_(1)277 if (elementFlags & Cocoa.NSTimeZoneDatePickerElementFlag) != 0:278 self.timeElementChecks.selectCellWithTag_(2)279 # date elements280 if (elementFlags & Cocoa.NSYearMonthDatePickerElementFlag) != 0:281 self.dateElementChecks.selectCellWithTag_(0)282 if (elementFlags & Cocoa.NSYearMonthDayDatePickerElementFlag) != 0:283 self.dateElementChecks.selectCellWithTag_(1)284 if (elementFlags & Cocoa.NSEraDatePickerElementFlag) != 0:285 self.dateElementChecks.selectCellWithTag_(2)286 @objc.IBAction287 def setMinDate_(self, sender):288 self.datePickerControl.setMinDate_(self.minDatePicker.dateValue())289 self.setMinDateButton.setEnabled_(False)290 self.clearMinDateButton.setEnabled_(True)291 @objc.IBAction292 def setMaxDate_(self, sender):293 self.datePickerControl.setMaxDate_(self.maxDatePicker.dateValue())294 self.setMaxDateButton.setEnabled_(False)295 self.clearMaxDateButton.setEnabled_(True)296 @objc.IBAction297 def clearMinDate_(self, sender):298 self.datePickerControl.setMinDate_(NSDate.distantPast())299 self.clearMinDateButton.setEnabled_(False)300 self.setMinDateButton.setEnabled_(True)301 @objc.IBAction302 def clearMaxDate_(self, sender):303 self.datePickerControl.setMaxDate_(NSDate.distantFuture())304 self.clearMaxDateButton.setEnabled_(False)305 self.setMaxDateButton.setEnabled_(True)306 @objc.IBAction307 def setDatePickerMode_(self, sender):308 tag = sender.selectedCell().tag()309 if tag == kSingleDateMode:310 self.datePickerControl.setDatePickerMode_(NSSingleDateMode)311 elif tag == kRangeDateMode:312 self.datePickerControl.setDatePickerMode_(NSRangeDateMode)313 self.updateControls() # force update of all UI elements and the picker itself314 def updateDatePickerMode(self):315 mode = self.datePickerControl.datePickerMode()316 if mode == Cocoa.NSSingleDateMode:317 self.datePickerModeRadios.selectCellWithTag_(0)318 # interval value not applicable:319 self.secondsRangeEdit.setEnabled_(False)320 self.secondsRangeEditLabel.setTextColor_(Cocoa.NSColor.lightGrayColor())321 self.datePickerControl.setTimeInterval_(0)322 elif mode == Cocoa.NSRangeDateMode:323 self.datePickerModeRadios.selectCellWithTag_(1)324 # interval value applies:325 self.secondsRangeEdit.setEnabled_(True)326 self.secondsRangeEditLabel.setTextColor_(Cocoa.NSColor.blackColor())327 # set the date range by start date (here we use the current date in the date picker328 # control), and time interval (in seconds)329 secsStr = self.secondsRangeEdit.stringValue()330 numSeconds = int(secsStr)331 self.datePickerControl.setTimeInterval_(numSeconds)332 def controlTextDidEndEditing_(self, notification):...
selection.py
Source:selection.py
1from rubicon.objc import CGSize, objc_method2from travertino.size import at_least3from toga_iOS.libs import UIColor, UIPickerView, UITextBorderStyle, UITextField4from toga_iOS.widgets.base import Widget5class TogaPickerView(UIPickerView):6 @objc_method7 def numberOfComponentsInPickerView_(self, pickerView) -> int:8 return 19 @objc_method10 def pickerView_numberOfRowsInComponent_(self, pickerView, component: int) -> int:11 return len(self.interface.items)12 @objc_method13 def pickerView_titleForRow_forComponent_(self, pickerView, row: int, component: int):14 return str(self.interface.items[row])15 @objc_method16 def pickerView_didSelectRow_inComponent_(self, pickerView, row: int, component: int):17 self.native.text = self.interface.items[row]18 if self.interface.on_select:19 self.interface.on_select(self.interface)20class Selection(Widget):21 def create(self):22 self.native = UITextField.alloc().init()23 self.native.interface = self.interface24 self.native.tintColor = UIColor.clearColor25 self.native.borderStyle = UITextBorderStyle.RoundedRect26 self.picker = TogaPickerView.alloc().init()27 self.picker.interface = self.interface28 self.picker.native = self.native29 self.picker.delegate = self.picker30 self.picker.dataSource = self.picker31 self.native.inputView = self.picker32 self.native.delegate = self.picker33 self.add_constraints()34 def rehint(self):35 # Height of a text input is known.36 fitting_size = self.native.systemLayoutSizeFittingSize(CGSize(0, 0))37 self.interface.intrinsic.width = at_least(fitting_size.width)38 self.interface.intrinsic.height = fitting_size.height39 def remove_all_items(self):40 # No special handling required41 pass42 def add_item(self, item):43 if not self.native.text:44 self.native.text = item45 def select_item(self, item):46 self.interface.factory.not_implemented('Selection.select_item()')47 def get_selected_item(self):48 return self.interface.items[self.picker.selectedRowInComponent(0)]49 def set_on_select(self, handler):50 # No special handling required...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!