小编Hol*_*olt的帖子

Tkinter Canvas将项目移至顶级

我有一个Tkinter Canvas小部件(Python 2.7,而不是3),在这个Canvas上我有不同的项目.如果我创建一个与旧项目重叠的新项目,它将在前面.我现在如何在旧创建的项目前面移动旧项目,或者甚至在"画布"上的所有其他项目前移动旧项目?

目前为止的示例代码:

from Tkinter import *
root = Tk()
canvas = Canvas(root,width=200,height=200,bg="white")
canvas.grid()
firstRect = canvas.create_rectangle(0,0,10,10,fill="red")
secondRect = canvas.create_rectangle(5,5,15,15,fill="blue")
Run Code Online (Sandbox Code Playgroud)

现在我希望firstRect位于secondRect之前.

python tkinter widget tkinter-canvas

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

如何从Ada源构建一个可从C++代码调用的静态库?

我需要构建一个静态库,其中包含一堆用Ada编写的代码,可以用C/C++编写的代码调用.

我已经通过互联网搜索,得到了一定的了解gnatmake,gnatbindgnatlink,但仍无法把工作做得正确.

另外,我读过有些工具依赖于某种项目文件.我对那些不感兴趣,我只需要一堆命令来编写Makefile.

c++ ada static-libraries

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

如果我在之前添加+而不是之后添加+,字符串(如"123")如何转换为数字?

当我在引号之类的数字之前添加+时+"123",它会转换为typeof number但是如果我添加like "123"+,则它正在等待下一个操作数.为什么?为什么在第一种情况下它转换为数字?

javascript

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

用颜色填充 Tkinter 画布对象之间的空间

Python 2.7.8、Windows 7

我对 Tkinter 画布小部件进行了子类化,并添加了一个新方法来创建具有圆角边缘的矩形。

import Tkinter as tk

class MyCanvas(tk.Canvas):
    def __init__(self, *args, **kwargs):
        tk.Canvas.__init__(self, *args, **kwargs)

    def create_rounded(self, x1, y1, x2, y2, r):
        self.create_arc(x1, y1, x1+r, y1+r, start=90, extent=90, style=tk.ARC)
        self.create_arc(x2-r, y1, x2, y1+r, start=0, extent=90, style=tk.ARC)
        self.create_arc(x1, y2-r, x1+r, y2, start=180, extent=90, style=tk.ARC)
        self.create_arc(x2-r, y2-r, x2, y2, start=270, extent=90, style=tk.ARC)
        self.create_line(x1+r/2, y1, x2-r/2, y1)
        self.create_line(x1, y1+r/2, x1, y2-r/2)
        self.create_line(x1+r/2, y2, x2-r/2, y2)
        self.create_line(x2, y1+r/2, x2, y2-r/2)
Run Code Online (Sandbox Code Playgroud)

我想用单一颜色填充我创建的圆角矩形。我该怎么办呢。

这是我使用一些参数运行时得到的结果

这就是我要的

python tkinter tkinter-canvas

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

当__init__.py存在时,带有安装程序的Python构建cython扩展会创建子文件夹

我正在尝试cython使用以下命令编译一个简单的模块setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("verifier_c.pyx"),
)
Run Code Online (Sandbox Code Playgroud)

我具有以下文件夹结构:

.
c_ext/
  __init__.py
  verifier_c.pyx
  setup.py
Run Code Online (Sandbox Code Playgroud)

如果我运行以下命令:

python setup.py build_ext --inplace
Run Code Online (Sandbox Code Playgroud)

我得到一个额外的c_ext子文件夹,如下所示:

.
c_ext/
  build/
    ...
  c_ext/
    verifier_c.so
  __init__.py
  verifier_c.pyx
  setup.py
Run Code Online (Sandbox Code Playgroud)

但是,如果删除该__init__.py文件,则该verifier_c.so文件将与存放在同一文件夹中verifier_c.pyx

我没有找到记录此行为的位置,但是我想保留verifier_c.so在与该文件夹相同的文件夹中,verifier_c.pyx而不必__init__.py每次运行时都删除它setup.py。我该如何实现?

python cython setup.py

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

为什么链接到librt在g ++和clang之间交换性能?

我刚从@ tony-d 找到了这个答案,用一个基准代码来测试虚函数调用开销.我查了基准使用g++:

$ g++ -O2 -o vdt vdt.cpp -lrt
$ ./vdt
virtual dispatch: 150000000 0.128562
switched: 150000000 0.0803207
overheads: 150000000 0.0543323
...
Run Code Online (Sandbox Code Playgroud)

我的表现更好(他的比例约为2),但后来我查了一下clang:

$ clang++-3.7 -O2 -o vdt vdt.cpp -lrt
$ ./vdt
virtual dispatch: 150000000 0.462368
switched: 150000000 0.0569544
overheads: 150000000 0.0509332
...
Run Code Online (Sandbox Code Playgroud)

现在这个比例上升到70左右!

然后我注意到了-lrt命令行参数,经过一些谷歌搜索后,librt我尝试了没有它g++clang:

$ g++ -O2 -o vdt vdt.cpp
$ ./vdt
virtual dispatch: 150000000 0.4661
switched: 150000000 0.0815865
overheads: 150000000 0.0543611
...
$ …
Run Code Online (Sandbox Code Playgroud)

c++ benchmarking librt

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

在类似函数的函数中转发和返回类型

我需要创建一个reduce类似的函数std::reduce,但不是处理容器,这个函数应该适用于可变参数.

这就是我目前拥有的:

template <typename F, typename T>
constexpr decltype(auto) reduce(F&&, T &&t) {
    return std::forward<T>(t);
}

template <typename F, typename T1, typename T2, typename... Args>
constexpr decltype(auto) reduce(F&& f, T1&& t1, T2&& t2, Args&&... args) {
    return reduce(
        std::forward<F>(f),
        std::forward<F>(f)(std::forward<T1>(t1), std::forward<T2>(t2)),
        std::forward<Args>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

以下按预期工作:

std::vector<int> vec;
decltype(auto) u = reduce([](auto &a, auto b) -> auto& {
        std::copy(std::begin(b), std::end(b), std::back_inserter(a));
        return a;
    }, vec, std::set<int>{1, 2}, std::list<int>{3, 4}, std::vector<int>{5, 6});

assert(&vec == &u); // ok
assert(vec == …
Run Code Online (Sandbox Code Playgroud)

c++ templates temporary template-meta-programming c++14

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

同时获取steady_clock和system_clock

我的代码如下所示:

// Next 2 lines should be at the same time
auto nowMonotonic = std::chrono::steady_clock::now();
auto nowSystem = std::chrono::system_clock::now();

// Do some calc with nowMonotonic and nowSystem
Run Code Online (Sandbox Code Playgroud)

这可能会发生:

auto nowMonotonic = std::chrono::steady_clock::now();
// Some other thread/process interrupts this thread for an 
// unspecific amount of time...
auto nowSystem = std::chrono::system_clock::now();

// Do some calc with nowMonotonic and nowSystem
Run Code Online (Sandbox Code Playgroud)

我正在(非实时)Linux 系统上工作。我正在根据 currentsteady_clock和 current进行一些计算system_clock。结果优于两个时钟读数之间的抖动。

(几乎)同时读取两个时钟的最佳方法是什么?

是否有特殊的系统调用来执行此操作?是否有可能在单个系统调用中获得这两个时钟的差异?

c++ synchronization c++-chrono

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

使用带有参数类型的可变参数模板在c ++中获取函数在编译时的参数个数

说我有一节课:

template<typename... Types>
class Example
{
public:
    using types = std::tuple<Types...>;
    template<size_t N> using dim_type = std::tuple_element_t<N, types>;
};
Run Code Online (Sandbox Code Playgroud)

我想实现一个依赖于元组元素的成员函数,如下所示,目标是在编译期间访问参数的数量:

template<size_t N>
inline constexpr void do_stuff(const dim_type<N>& elems...)
{
    constexpr size_t size_stuff = sizeof...(elems); // <-- this is the end goal 
};
Run Code Online (Sandbox Code Playgroud)

问题是这个实现do_stuff()不会做.最终我希望这个功能像这样工作:

Example<long,std::string> ex;
ex.do_stuff<0>(3);
ex.do_stuff<0>(5,6);
ex.do_stuff<1>("a","b","c","d");
ex.do_stuff<0>("a","b","c","d"); // <-- this line should not compile
ex.do_stuff<1>(1,"b"); // <-- nor should this one
Run Code Online (Sandbox Code Playgroud)

在内部do_stuff我应该在编译时知道传递了多少个参数.

一个可能的答案,但我遇到了问题:

我认为正确实现这一点需要使用可变参数模板,但是,我在使std::enable_if部件工作时遇到问题:

template<size_t N, typename... Args,
    std::enable_if_t<static_and<std::is_convertible_v<Args, dim_type<N>>...>::value>* = nullptr> …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae template-meta-programming variadic-templates c++14

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

实现高效的算术运算符

我正在实现一个expr复制起来并不便宜的类,并且我想实现适当的“高效”算术运算符。对于这个问题,我将重点关注operator-

class expr {
public:
    expr(expr const&) = default;
    expr(expr &&) = default;

    expr operator-() const& { return -expr(*this); }
    expr operator-() && { 
        // Perform operations in-place.
        return std::move(*this);
    }

    expr& operator-=(expr const& other) & {
        // In-place operations.
        return *this;
    }

    expr&& operator-=(expr const& other) && {
        // In-place operations.
        return std::move(*this); // Do I really need this move?
    }
};

expr operator-(expr const& lhs, expr const& rhs) {
    return expr(lhs) -= rhs;
} …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
0
解决办法
112
查看次数