AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-884463

David Tonhofer's questions

Martin Hope
David Tonhofer
Asked: 2025-02-12 19:17:16 +0800 CST

当我创建一个 Numpy 浮点数数组时,我得到一个 Python 浮点数数组

  • 6

代码:

import sys
import numpy as np

print(f"We are using Python {sys.version}", file=sys.stderr)
print(f"We are using numpy version {np.__version__}", file=sys.stderr)  # 2.2.1

def find_non_numpy_floats(x: any) -> bool:
    if not (isinstance(x, np.float64)):
        print(f"Found non-numpy.float64: {x} of type {type(x)}", file=sys.stderr)
        return False
    else:
        return True


w: np.ndarray = np.zeros((2, 2), dtype=np.float64)

np.vectorize(lambda x: find_non_numpy_floats(x))(w)

assert (np.all(np.vectorize(lambda x: isinstance(x, np.float64))(w))), "try to keep using the numpy floats"

我期望Numpy.zeros生成一个 Numpy 数组float64,如果我理解正确的话,它与 Python 不同float(IEEE 64 位浮点数与 Python 特有的某些东西?)

然而,上述结果是:

We are using Python 3.13.1 (main, Dec  9 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
We are using numpy version 2.2.1
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>

以及一个断言错误。

为什么会这样?我该如何解决这个问题(我应该这样做吗?)

python
  • 1 个回答
  • 55 Views
Martin Hope
David Tonhofer
Asked: 2024-10-06 22:20:30 +0800 CST

JavaFX 事件发送到按钮控件:为什么有两个 MOUSE_ENTERED / MOUSE_EXITED 事件?

  • 9

我对根据鼠标指针发生的情况发送到 JavaFX按钮控件的事件进行了一些实验,并从中生成了一个分层状态机图(近似,因为我的编辑器没有任何语义概念):

这一切都非常简单,只需从起始状态跟踪行为并查看会发生什么。这里有一个微妙之处,因为按钮控件内的标签也起着作用,它会生成MOUSE_ENTERED_TARGET事件MOUSE_EXITED_TARGET(我向 ChatGPT 询问了这个问题,它打印了一个非常漂亮的答案,但答案是错的😂)

按钮控制分层状态机

我唯一想知道的一点是——为什么我会持续收到两件MOUSE_ENTERED或两个MOUSE_EXITED事件,一个带有consumed = false,一个带有consumed = true?

示例代码

以下是(接近最小的)示例。它只是创建了一个按钮,我们可以手动点击它来查看生成了哪些事件:

文件com.example.stack_overflow.Main.java

package com.example.stack_overflow;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Objects;

class PaneBuilder {

    private final StackPane stackPane;

    private static void log(String text) {
        System.out.println(text);
    }

    public PaneBuilder() {
        stackPane = buildCenteringStackPaneAroundButton(buildButton());
    }

    private static Button buildButton() {
        final var button = new Button("Click Me!");
        button.setMinWidth(120); // I think these are "120pt", why is there no "unit"?
        final var desc = "Button";
        button.addEventHandler(ActionEvent.ACTION, event -> {
            handleActionEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_ENTERED, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_EXITED, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, event -> {
            handleMouseEvent(desc, event);
        });
        button.addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, event -> {
            handleMouseEvent(desc, event);
        });
        button.armedProperty().addListener((obs, oldVal, newVal) -> log(desc + ": armed status changes: " + oldVal + " -> " + newVal));
        return button;
    }

    public Pane getPane() {
        return stackPane;
    }

    private static StackPane buildCenteringStackPaneAroundButton(Button button) {
        final var stackPane = new StackPane();
        stackPane.setAlignment(Pos.CENTER);
        final var hbox = buildHBoxAroundVBoxAroundButton(button);
        final var insets = new Insets(20, 20, 20, 20); // I guess those are "20points"
        StackPane.setMargin(hbox, insets);
        stackPane.getChildren().add(hbox);
        return stackPane;
    }

    private static HBox buildHBoxAroundVBoxAroundButton(Button button) {
        final var hbox = new HBox();
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(buildVBoxAroundButton(button));
        return hbox;
    }

    private static VBox buildVBoxAroundButton(Button button) {
        final var vbox = new VBox();
        vbox.setAlignment(Pos.CENTER);
        vbox.getChildren().add(button);
        return vbox;
    }

    private static void appendEventDesc(StringBuilder buf, Event event) {
        buf.append("\n   Type          : " + event.getEventType());
        buf.append("\n   Class         : " + event.getClass().getName());
        buf.append("\n   Consumed      : " + event.isConsumed());
        buf.append("\n   Source class  : " + event.getSource().getClass().getName());
        buf.append("\n   Event         : " + Objects.toIdentityString(event));
    }

    public static void handleMouseEvent(String desc, MouseEvent event) {
        final var buf = new StringBuilder("Mouse event in " + desc);
        appendEventDesc(buf, event);
        log(buf.toString());
    }

    public static void handleActionEvent(String desc, ActionEvent event) {
        final var buf = new StringBuilder("Action event in " + desc);
        appendEventDesc(buf, event);
        log(buf.toString());
    }
}

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("Button Example");
        stage.setScene(new Scene(new PaneBuilder().getPane()));
        stage.sizeToScene();
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

以及构建和运行上述程序的 POM。该程序必须使用JavaFX Maven 插件javafx:run的Maven 目标运行

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>stack_overflow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- RUNNING VIA MAVEN + JAVAFX PLUGIN IN IDE: -->
    <!--   'Main Menu' > 'Run' > 'Run Maven Goal' > 'Plugins' > 'JavaFx Maven Plugin' > 'javafx:run' -->
    <!--   This will invoke the goal "javafx:run" of the "javafx-maven-plugin". -->
    <!--   With the 'Run New Maven Goal' menu entry, you can define that goal, and it will appear in the -->
    <!--   context menu as 'right-mouse-button menu' > 'run maven' > 'javafx:run' -->

    <!-- RUNNING VIA MAVEN IN COMMAND LINE w/o leaving the IDE (no need for OpenJDX SDK): -->
    <!-- Right-click on the project window and select "Open Terminal at the current Maven module path". -->
    <!-- Enter the command "mvn javafx:run" or for debugging output "mvn -X javafx:run". -->
    <!-- This will invoke the goal "javafx:run" of the "javafx-maven-plugin". -->

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- This is the latest OpenJavaFX version on 2024-09-26 (version of 2024-09-16) -->
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->

        <javafx.version>23</javafx.version>

        <javafx.plugin.version>0.0.8</javafx.plugin.version>
        <compiler.plugin.version>3.13.0</compiler.plugin.version>
        <exec.plugin.version>3.4.1</exec.plugin.version>
        <dependency.plugin.version>3.8.0</dependency.plugin.version>
        <main.class>com.example.stack_overflow.Main</main.class>

        <java.compiler.source.version>21</java.compiler.source.version>
        <java.compiler.target.version>21</java.compiler.target.version>

    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <!-- Standard Java Compiler Plugin -->
            <!-- https://maven.apache.org/plugins/maven-compiler-plugin/ -->
            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler.plugin.version}</version>
                <configuration>
                    <source>${java.compiler.source.version}</source>
                    <target>${java.compiler.target.version}</target>
                </configuration>
            </plugin>

            <!-- Special Plugin to run JavaFX programs -->
            <!-- https://github.com/openjfx/javafx-maven-plugin -->
            <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-maven-plugin -->
            <!-- If you run the plugin's goal on the command line with the '-X' option like so: -->
            <!-- mvn -X javafx:run -->
            <!-- you will see the command line the plugin builds, which looks as follows, with ++ replaced by double dash -->

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.plugin.version}</version>
                <configuration>
                    <mainClass>${main.class}</mainClass>
                    <options>
                        <option>-ea</option>
                    </options>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

运行该程序会出现以下窗口:

在此处输入图片描述

如果我们现在用鼠标指针沿着指示的红色路径移动,然后单击鼠标并再次将鼠标指针移出按钮:

在此处输入图片描述

我们得到了属性值armed和按钮发出的事件的以下变化。请注意双引号MOUSE_ENTERED= consumed,false然后是true。

进入按钮控件:

Mouse event in Button
   Type          : MOUSE_ENTERED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@2ebee20a
Mouse event in Button
   Type          : MOUSE_ENTERED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : true
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@2ebee20a

输入按钮控件的标签:

Mouse event in Button
   Type          : MOUSE_ENTERED_TARGET
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@5d41df9f

单击,注意状态的变化armed。

Mouse event in Button
   Type          : MOUSE_PRESSED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@3df998ed
Button: armed status changes: false -> true
Mouse event in Button
   Type          : MOUSE_RELEASED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@241a11d5
Action event in Button
   Type          : ACTION
   Class         : javafx.event.ActionEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.event.ActionEvent@3d8fce01
Button: armed status changes: true -> false
Mouse event in Button
   Type          : MOUSE_CLICKED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@505036ef

退出按钮控件的标签:

Mouse event in Button
   Type          : MOUSE_EXITED_TARGET
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@2d496725

退出按钮控制:

Mouse event in Button
   Type          : MOUSE_EXITED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : false
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@59a94fe0
Mouse event in Button
   Type          : MOUSE_EXITED
   Class         : javafx.scene.input.MouseEvent
   Consumed      : true
   Source class  : javafx.scene.control.Button
   Event         : javafx.scene.input.MouseEvent@59a94fe0
javafx
  • 2 个回答
  • 54 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve