小编Sam*_*ane的帖子

使用线程和 tkinter 模块在 python 中停止线程并避免“RuntimeError”的最佳方法是什么?

我在网上经历了多种解决方案,但它们需要大量代码,一旦您扩展,这些代码可能会令人困惑。是否有一种简单的方法可以停止线程并避免RuntimeError: threads can only be started once, 以便无限次调用线程。这是我的代码的简单版本:

import tkinter
import time
import threading

def func():

    entry.config(state='disabled')
    label.configure(text="Standby for  seconds")
    time.sleep(3)
    sum = 0
    for i in range(int(entry.get())):
        time.sleep(0.5)
        sum = sum+i
        label.configure(text=str(sum))
    entry.config(state='normal')

mainwindow = tkinter.Tk()
mainwindow.title('Sum up to any number')

entry = tkinter.Entry(mainwindow)
entry.pack()
label = tkinter.Label(mainwindow, text = "Enter an integer",font=("Arial",33))
label.pack()

print(entry.get())

button = tkinter.Button(mainwindow, text="Press me", command=threading.Thread(target=func).start)
button.pack()
Run Code Online (Sandbox Code Playgroud)

python multithreading tkinter software-design python-multithreading

5
推荐指数
1
解决办法
118
查看次数

Django QueryDict 中的键列表返回一个元素而不是整个列表

request.POST我正在使用 Django,并且从我的视图进行访问。代码如下:

\n
data = request.POST\nprint(data)\n
Run Code Online (Sandbox Code Playgroud)\n

返回:

\n
<QueryDict: {\'name\': [\'Sam\'], \'phone\': [\'+10795524594\'], \'message\': [\'Es-s\xc3\xa9nia\'], \'Coupon\': [\'\'], \'csrfmiddlewaretoken\': [\'xcGnoJOtnAmXcUBXe01t7ItuMC8BAFHE\n6H9Egqd8BuooxLbp3ZrqvwzTZAxukMJW\', \'xcGnoJOtnAmXcUBXe01t7ItuMC8BAFHE6H9Egqd8BuooxLbp3Zrq``vwzTZAxukMJW\'], \'Size\': [\'S\', \'M\']}>\n
Run Code Online (Sandbox Code Playgroud)\n

但是,无论使用.dict()method 还是 using data.get("Size"),我都只得到一个元素;不是整个列表。我怎样才能解决这个问题?

\n

python django django-templates django-forms

5
推荐指数
1
解决办法
2296
查看次数

无法写入 y/n 来继续在 kaggle 中安装库

我想在kaggle中安装gmaps库,不幸的是,当编写!conda install -c conda-forge gmaps安装开始时,但要求我继续y/n,没有输入写y或n。

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs:
    - gmaps


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2019.11.28         |           py36_0         149 KB  conda-forge
    conda-4.8.2                |           py36_0         3.0 MB  conda-forge
    geojson-2.5.0              |             py_0          15 KB  conda-forge
    gmaps-0.9.0                |             py_0         1.7 MB  conda-forge
    ------------------------------------------------------------
                                           Total:         4.9 MB

The following NEW packages will be INSTALLED:

  geojson            conda-forge/noarch::geojson-2.5.0-py_0
  gmaps              conda-forge/noarch::gmaps-0.9.0-py_0

The following packages …
Run Code Online (Sandbox Code Playgroud)

python git git-bash jupyter-notebook kaggle

2
推荐指数
1
解决办法
3986
查看次数

如何从 drf-spectaulous API 文档中删除 RetrieveUpdateAPIView 中的 put 方法?

我有以下观点:

class PersonalInfos(generics.RetrieveUpdateAPIView):
    serializer_class = ClientSerializer
    permission_classes = [IsAuthenticated]
    def get_queryset(self):
        """
        :return: A QuerySet Object
        """
        return Client.objects.get(user=self.request.user)

    def get(self, *args):
        """
        :param args: Handled by rest_framework views.dispatch

        :return: JSON object containing User Personal Data
        """
        queryset = self.get_queryset()
        serializer = ClientSerializer(queryset)
        return Response(data=serializer.data)

    def patch(self, request):
        """
        :param request: request object is sent by the client

        :return:  Json response with the data sent of the body
        """

        client = self.get_queryset()
        serializer = ClientSerializer(client, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save() …
Run Code Online (Sandbox Code Playgroud)

api django documentation django-rest-framework drf-yasg

2
推荐指数
1
解决办法
2430
查看次数

如何在 Django 中处理 Firebase 云消息通知?

我正在设法通过 Django 将 FCM 通知发送到我的应用程序。这是我的推送通知功能:

import firebase_admin
from firebase_admin import credentials, messaging

cred = credentials.Certificate("service-key.json")
firebase_admin.initialize_app(cred)

def send_push(title, msg, registration_token, dataObject=None):
    try:
        message = messaging.MulticastMessage(
            notification=messaging.Notification(
                title=title,
                body=msg,
            ),
            data=dataObject,
            tokens=registration_token,
        )

        response = messaging.send_multicast(message)
    except messaging.QuotaExceededError:
        raise f'Exceeding FCM notifications quota'

Run Code Online (Sandbox Code Playgroud)

现在,我将在视图内发送通知:

class AdminChangeRole(viewsets.Viewset):
    serializer_class = SomeSerializer
    permission_classes = [IsAdminOnly]
    def post(self, request, *args, **kwargs):
        # code that will change the role of a user
        send_push("Role changed", f"You role was set to {self.role}", fcm_registration_token)
        # return Response
Run Code Online (Sandbox Code Playgroud)

现在,在序列化器中发布一些数据并保存它之后。我希望向用户发送通知。但是,我想知道我是否正确处理了这个问题,包括并行 2500 …

python django firebase python-asyncio firebase-cloud-messaging

2
推荐指数
1
解决办法
3167
查看次数