我目前正在尝试from ttkthemes import ThemedStyle
import tkinter as tk
from tkinter import ttk
app = tk.Tk()
app.title('App')
style = ThemedStyle(app)
style.set_theme("arc")
tktext = tk.Label(app, text=" tk Label")
tktext.pack()
tkbutton = tk.Button(app, text="tk Button")
tkbutton.pack()
text = ttk.Label(app, text=" ttk Label")
text.pack()
button = ttk.Button(app, text="ttk Button")
button.pack()
app.geometry('200x200')
app.mainloop()
,最近遇到CLLocation
。如果用户允许应用使用位置服务,我想显示一个视图,如果用户不允许,则显示另一个视图。除了authorizationStatus
以外,它都可以按我的意愿工作。如果我将其插入.notDetermined
块中并按“允许使用时”,则无法渲染“允许”页面,而必须重新启动应用程序以渲染它。如果我将其放在.denied
块中并按“拒绝”,则也是相反的方法。所以我的问题是,使用此命令有什么好的做法?理想情况下,我希望该应用程序等待用户实际做出选择并从那里加载它,但是由于需要.accesWhenInUse
来处理,所以我有点迷路。
到目前为止,我的初始化位置函数:
.notDetermined
我现在根据自己的情况提取了 private func initLocation() {
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse, .authorizedAlways :
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
break
case .notDetermined:
break
case .restricted, .denied:
print("well this is awkward..")
view.addSubview(noLocationTextView)
NSLayoutConstraint.activate([
noLocationTextView.heightAnchor.constraint(equalToConstant: 210),
noLocationTextView.widthAnchor.constraint(equalToConstant: 210),
noLocationTextView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
noLocationTextView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
noLocationTextView.text = "Well we're going to need your location"
break
}
}
}
。
所以问题是,我该如何处理,以便应用程序在做出选择时等待并加载正确的视图?
答案 0 :(得分:0)
由于@ Paulw11,我设法使用delegate method来解决此问题。因此,我没有使用initLocation()
,而是使用了:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse, .authorizedAlways:
goToLocationSettings.removeFromSuperview()
noLocationContainer.removeFromSuperview()
break
case .denied, .restricted:
print("well this is awkward..")
view.addSubview(noLocationContainer)
NSLayoutConstraint.activate([
noLocationContainer.heightAnchor.constraint(equalTo: self.view.heightAnchor),
noLocationContainer.widthAnchor.constraint(equalTo: self.view.widthAnchor),
])
noLocationContainer.insertSubview(goToLocationSettings, at: 3)
NSLayoutConstraint.activate([
goToLocationSettings.widthAnchor.constraint(equalToConstant: 300),
goToLocationSettings.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
goToLocationSettings.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 200),
goToLocationSettings.heightAnchor.constraint(equalToConstant: 50)
])
break
default:
break
}
}
这种方式.notDetermined
是default
,直到用户做出我想要的选择之前,它什么都不做。它也会实时更改,因此,如果我拒绝访问位置,然后在应用程序运行时授予它,则它只会更新视图并显示内容。希望这对遇到类似问题的人有所帮助!