小编unl*_*kme的帖子

如何使用tkinter中的按钮设置`Entry`小部件的文本/值/内容

我正在尝试Entry使用tkinter模块使用GUI中的按钮设置窗口小部件的文本.

该GUI用于帮助我将数千个单词分为五类.每个类别都有一个按钮.我希望使用一个按钮会显着加快我的速度,我想每次都仔细检查这些单词,否则我只会使用按钮并让GUI处理当前单词并带来下一个单词.

由于某种原因,命令按钮的行为不像我想要的那样.这是一个例子:

win = Tk()

v = StringVar()
def setText(word):
    v.set(word)

a = Button(win, text="plant", command=setText("plant")
a.pack()
b = Button(win, text="animal", command=setText("animal"))
b.pack()
c = Entry(win, textvariable=v)
c.pack()
win.mainloop()
Run Code Online (Sandbox Code Playgroud)

到目前为止,当我能够编译时,点击什么都不做.

python events tkinter tkinter-entry

19
推荐指数
3
解决办法
10万
查看次数

在 Django 中模拟一个模型字段验证器

根据python模拟库的文档。我们从正在使用/调用它的模块中模拟出一个函数。

a.py
def function_to_mock(x):
   print('Called function to mock')

b.py
from a import function_to_mock

def function_to_test(some_param):
    function_to_mock(some_param)

# According to the documentation 
#if we want to mock out function_to_mock in a
# test we have to patch it from the b.py module because that is where 
# it is called from

class TestFunctionToTest(TestCase):

    @patch('b.function_to_mock')
    def test_function_to_test(self, mock_for_function_to_mock):
        function_to_test()
        mock_for_function_to_mock.assert_called_once()
   
# this should mock out function to mock and the assertion should work

Run Code Online (Sandbox Code Playgroud)

我陷入了一种无法确切说出如何模拟相关函数的情况。这是情况。

# some application
validators.py
def validate_a_field(value):
    # do …
Run Code Online (Sandbox Code Playgroud)

python django unit-testing mocking

9
推荐指数
1
解决办法
407
查看次数

删除/清除白噪声中的静态文件缓存

我似乎无法找到如何刷新白噪声静态文件缓存。它一直给我带来一个问题。即使我删除了导致问题的特定文件,这种情况仍然存在。清单仍然提到丢失的已删除静态文件。我想清除缓存以便可以重新填充它。我正在使用带有 django 的 docker-container

django django-staticfiles docker whitenoise

6
推荐指数
1
解决办法
563
查看次数

Serializer等效于rest框架中django表单的clean

什么是django表单的clean方法的序列化程序等效项。

我的用例是我想对模型序列化程序类的某些字段进行自定义模型字段验证。我的猜测可能是该框架的run_validators。但是我不确定。

谢谢,问候,

django serialization django-forms django-rest-framework

4
推荐指数
1
解决办法
1614
查看次数

在 django 中与单元测试分开运行集成测试

在 django 中创建测试的约定是将测试放置在名为 的模块中tests_*.py,然后以python manage.py test.

这将运行所有名为测试的模块中定义的测试。

我遇到的挑战是集成测试可能需要大量的资源设置,例如与外部服务的连接。我想在集成测试中模拟这些服务会导致集成测试失去其意义。

因此,我询问仅运行单元测试以及仅在单元测试正常运行时运行集成测试的最佳实践。

我能想到的唯一方法是放置integration tests在以不同模式命名的文件中integration_*.py,然后在运行django 文档指定的集成测试时使用模式参数

像这样python manage.py test --pattern="integration_*"

这样当python manage.py test调用集成测试时就会被忽略。

有没有人有建议或者推荐。

python django unit-testing

4
推荐指数
1
解决办法
881
查看次数

使用 AutoSlugField 填充现有模型对象

我需要为已经存在的模型对象填充 AutoslugField。我的错误意识到 slug 字段非常方便,而且比使用 pk 用于安全目的更好。

我已经在数据库中有模型对象(行)。我想向它们添加 AutoSlugField。

任何人都知道我如何实现这一目标。

谢谢

django django-extensions

3
推荐指数
1
解决办法
1276
查看次数

在自定义迁移中将模型导入为“apps.get_model('app_name', 'ModelName')”的逻辑是什么

我问这个问题是因为当我使用使用MPPT模型的django-hordak时,我遇到了django.db.utils.IntegrityError: null value in column \xe2\x80\x9clft\xe2\x80\x9d违反非空约束

\n\n

提出的一些解决方案虽然hacky违背了惯例,但确实有效。

\n\n

基础

\n\n

在 django 中,当编写自定义迁移时,例如创建基线数据时。

\n\n
def forward_func(apps_registry, schema_editor):\n    Model = apps_registry.get_model(\'app_name\', \'ModelName\')\n    Model.objects.create(**{\'field_1\': \'field_1\', \'field_2\': \'field_2\')\n    # Do whatever you want with my Model \n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的情况下,django-hordak 具有帐户模型。.objects.create(**data)\nraises the上面指定的数据库上的 IntegrityError 。

\n\n

建议的解决方案

\n\n

建议的解决方案之一是直接导入模型。

\n\n
def forward_func(apps_registry, schema_editor):\n    # Model = apps_registry.get_model(\'app_name\', \'ModelName\')\n    # lets forget about importing the standard way because it will raise integrity error.\n\n    from app_name.models import …
Run Code Online (Sandbox Code Playgroud)

django import django-migrations

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