小编con*_*dor的帖子

在一个处理程序中处理所有文本框事件

我知道如何处理表单中的文本框事件.但是想让这段代码更短,因为我会有30个文本框.使用它是低效的:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged
    Dim tb As TextBox = CType(sender, TextBox)

    Select Case tb.Name
        Case "TextBox1"
            MsgBox(tb.Text)
        Case "TextBox2"
            MsgBox(tb.Text)
    End Select
End Sub
Run Code Online (Sandbox Code Playgroud)

有没有办法缩短处理程序?

vb.net

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

在numpy数组中获取项目的邻居

我有一个看起来像这样的数组:

[['A0' 'B0' 'C0']
 ['A1' 'B1' 'C1']
 ['A2' 'B2' 'C2']]
Run Code Online (Sandbox Code Playgroud)

我想获得B1的邻居B0 , C1 , B2 , A1,以及其索引。

这是我想出的:

import numpy as np


arr = np.array([
    ['A0','B0','C0'],
    ['A1','B1','C1'],
    ['A2','B2','C2'],
])


def get_neighbor_indices(x,y):
    neighbors = []
    try:
        top = arr[y - 1, x]
        neighbors.append((top, (y - 1, x)))
    except IndexError:
        pass
    try:
        bottom = arr[y + 1, x]
        neighbors.append((bottom, (y + 1, x)))
    except IndexError:
        pass
    try:
        left = arr[y, x - 1]
        neighbors.append((left, (y, x - 1))) …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

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

Draw lines inside a square using PIL

I'm trying to recreate this image using Python and PIL.

在此处输入图片说明

This is the code I come upped with:

from PIL import Image, ImageDraw


def draw_lines(draw, points):
    new_points = []
    for idx, point in enumerate(points):
        x, y = point
        if idx != len(points) - 1:
            if idx == 0:
                x = x + 25
            elif idx == 1:
                y = y + 25
            elif idx == 2:
                x = x - 25
            elif idx == 3:
                y = y - 25 …
Run Code Online (Sandbox Code Playgroud)

python python-imaging-library

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

通过从左到右填充来更改按钮背景

我想在按下时更改按钮的背景,方法是从左到右填充颜色,就像水平进度条一样。

这是检测按住不放的代码:

lateinit var buttonView: View
val cdt = object : CountDownTimer(1000, 100) {
            override fun onTick(millisUntilFinished: Long) {

            }

            override fun onFinish() {
                //do something after being press by 1 sec.
            }
        }

        val onTouchListener = View.OnTouchListener { v, event ->
            buttonView = v
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    (v.background as AnimationDrawable).start()
                    cdt.start()
                }
                MotionEvent.ACTION_UP -> {
                    (v.background as AnimationDrawable).stop()
                    cdt.cancel()
                }

            }
            false
        }
Run Code Online (Sandbox Code Playgroud)

这是我用于按钮的背景:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    ...items with drawables which colors are in …
Run Code Online (Sandbox Code Playgroud)

java android kotlin

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

从图像中删除抗锯齿功能

我想从图像中删除抗锯齿功能。此代码将从图像中获取 4 种主要颜色,将每个像素与 4 种主要颜色进行比较并分配最接近的颜色。

import numpy as np
from PIL import Image

image = Image.open('pattern_2.png')
image_nd = np.array(image)
image_colors = {}

for row in image_nd:
    for pxl in row:
        pxl = tuple(pxl)
        if not image_colors.get(pxl):
            image_colors[pxl] = 1
        else:
            image_colors[pxl] += 1

sorted_image_colors = sorted(image_colors, key=image_colors.get, reverse=True)
four_major_colors = sorted_image_colors[:4]


def closest(colors, color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
    index_of_smallest = np.where(distances == np.amin(distances))
    smallest_distance = colors[index_of_smallest]
    return smallest_distance[0]


for y, …
Run Code Online (Sandbox Code Playgroud)

python numpy image-processing antialiasing python-imaging-library

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

仅使用一个事件处理程序按下时,在按钮内获取文本

我正在制作一个简单的计算器.我厌倦了在每个按钮中分配事件处理程序,这是低效的.

我只想要一个事件处理程序在按下时获取按钮内的文本.

怎么做?

  Private Sub num1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles co.Click
    numPressed = num1.Text
End Sub
Run Code Online (Sandbox Code Playgroud)

vb.net button

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

使用PHP显示完整的1-128位十进制到二进制

我知道这种接缝很容易,但我对这种特殊情况有疑问.我已经知道如何使用PHP将十进制转换为二进制,但我想显示完整的位序列,但令人惊讶的是,我不知道如何.

转换必须是这样的:

converting 127(decimal) to binary using PHP = 1111111. the bit sequence is 1-128 for every 
octet(IP Address) so this must output = 01111111 even 128 is not used.
2nd Example:
1(decimal) to binary = 01. Want to display the full 1-128 binary sequence even if
128,64,32,16,8,4,2 is not used it must be like this 00000001 not 01.
Run Code Online (Sandbox Code Playgroud)

这是我的PHP代码:

<?php

        $octet1 = $_POST["oct1"];
        $octet2 = $_POST["oct2"];
        $octet3 = $_POST["oct3"];
        $octet4 = $_POST["oct4"];
        echo decbin($octet1) ," ", decbin($octet2) ," ", …
Run Code Online (Sandbox Code Playgroud)

php binary

0
推荐指数
1
解决办法
1253
查看次数

mysql_query()里面的Php变量

我正在尝试使用CREATE DATABASE命令创建一个数据库,但它给了我一个错误.这是我的代码:

$db_usr = mysql_real_escape_string($_POST["email"]);
$con=mysql_connect("localhost","root");
if (! $con)
{
    die('Could not connect: ' . mysql_error());
}
else
{
    test();
}

function test()
{
    $sql = "CREATE DATABASE '$db_usr'";
    mysql_query($sql);
}
Run Code Online (Sandbox Code Playgroud)

它总是返回"未定义的变量"

php mysql

0
推荐指数
1
解决办法
100
查看次数