PyQt5编程(24):在窗口中布局组件—水平对齐和垂直对齐

PyQt5编程(24):在窗口中布局组件—水平对齐和垂直对齐,第1张

from PyQt5 import QtWidgets

import sys

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget() # 父窗口

window.setWindowTitle("QHBoxLayout")

window.resize(300, 60)

button1 = QtWidgets.QPushButton("1")

button2 = QtWidgets.QPushButton("2")

hbox = QtWidgets.QHBoxLayout()# 创建容器

hbox.addWidget(button1) # 添加组件

hbox.addWidget(button2)

window.setLayout(hbox)# 指定父组件

window.show()

sys.exit(app.exec_())

pyqt5&pythonGui入门教程(1)第一个窗口(1) 第一个窗口和代码详细注释: fromPyQt5importQtWidgets#从PyQt库导入QtWidget通用窗口类classmywindow(QtWidgets.QWidget):#自己建一个mywindows类,以class开头,mywindows是自己的类名,

1、继承AppWidgetProvider

我们编写的桌面Widget需要提供数据更新,这里就需用用到AppWidgetProvider,它里面有一些系统回调函数。提供更新数据的 *** 作。AppWidgetProvider是BrocastReceiver的之类,也就是说它其实本质是一个广播接收器。下面我们看看AppWidgetProvider的几个重要的回调方法

复制代码

代码如下:

class WidgetProvider extends

AppWidgetProvider

{

private static final String

TAG="mythou_Widget_Tag"

// 没接收一次广播消息就调用一次,使用频繁

public void

onReceive(Context context, Intent intent)

{

Log.d(TAG,

"mythou--------->onReceive")

super.onReceive(context,

intent)

}

// 每次更新都调用一次该方法,使用频繁

public void

onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)

{

Log.d(TAG, "mythou--------->onUpdate")

super.onUpdate(context, appWidgetManager, appWidgetIds)

}

//

没删除一个就调用一次

public void onDeleted(Context context, int[] appWidgetIds)

{

Log.d(TAG, "mythou--------->onDeleted")

super.onDeleted(context, appWidgetIds)

}

//

当该Widget第一次添加到桌面是调用该方法,可添加多次但只第一次调用

public void onEnabled(Context

context)

{

Log.d(TAG,

"mythou--------->onEnabled")

super.onEnabled(context)

}

// 当最后一个该Widget删除是调用该方法,注意是最后一个

public void

onDisabled(Context context)

{

Log.d(TAG,

"mythou--------->onDisabled")

super.onDisabled(context)

}

}

其中我们比较常用的是onUpdate和onDelete方法。我这里刷新时间使用了一个Service,因为要定时刷新服务,还需要一个Alarm定时器服务。下面给出我的onUpdate方法:

复制代码

代码如下:

public void onUpdate(Context context,

AppWidgetManager appWidgetManager, int[] appWidgetIds)

{

super.onUpdate(context, appWidgetManager, appWidgetIds)

Time time = new

Time()

time.setToNow()

//使用Service更新时间

Intent intent = new

Intent(context, UpdateService.class)

PendingIntent pendingIntent =

PendingIntent.getService(context, 0, intent, 0)

//使用Alarm定时更新界面数据

AlarmManager alarm =

(AlarmManager)context.getSystemService(Context.ALARM_SERVICE)

alarm.setRepeating(AlarmManager.RTC, time.toMillis(true), 60*1000,

pendingIntent)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11802270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存