Python tkinter 第一行未在正确的 y 位置绘制 [英] Python tkinter first line not drawing in correct y location

查看:37
本文介绍了Python tkinter 第一行未在正确的 y 位置绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一篇文章中,Brian Oakley 提供了一些代码99% 解决了我遇到的问题.当我去运行代码时,除了一个小例外,一切都运行得更好.图表不在正确的 y 位置.我尝试的任何方法都不起作用.我被难住了.最后今天早上我偶然发现了这个问题的答案,我不确定这是否是一个问题,很可能是 Frame,或者是什么交易.除了首先在屏幕上放置一个虚拟点之外,他们是否是解决问题的另一种解决方案,我正在徘徊.

In another post Brian Oakley provide some code that 99% solved an issue I was having. When I went to run the code everything was running much better with one small exception. The graph wasn't in the correct y location. Anything I was trying wasn't working. I was stumped. Finally this morning I stumbled into the answer to the problem and I'm not sure if this is a problem, quite possibly with Frame, or what the deal is. I'm wandering if their is another solution to the problem other than first putting a dummy point on the screen.

在下面的代码中,如果您删除 def graph_data(self): 第一行中的 #,一切正常.当您输入 # 并且最初不在屏幕上绘制虚拟点时,线条将不会显示在屏幕上的正确位置,这是明智的.是他们处理这个问题的另一种方式,因为它似乎总是在发生.我在 linux 上运行它,所以这甚至可能是问题的一部分......谁知道.奇怪的是,当我在我的程序中首先打印比例尺时,它总是被正确放置,但是当我去绘制数据时,横穿屏幕的水平线永远不会打印在正确的位置,除非我先添加一个虚拟点.

In the code below, if you remove the # in the first line of the def graph_data(self): everything works fine. When you have the # in and don't plot a dummy point on the screen initially the lines won't show up in the correct location on the screen, y wise. Is their another way of dealing with this as it seems to always be happening. I am running this on linux so that might even be part of the problem...who knows. Strangely enough when in my program I have the scale print first it always gets placed correctly but when I go to graph the data the horizontal line running across the screen never prints in the correct location unless I add a dummy point first.

import tkinter as tk

class Example(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.DrawArea = tk.Canvas(self, width=1300, height=600, background="black",
                                  borderwidth=0, highlightthickness=0)
        self.scale = tk.Canvas(self, width=30, height=600, background="black",
                               borderwidth=0, highlightthickness=0)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.DrawArea.xview)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.DrawArea.yview)
        self.DrawArea.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set)

        self.DrawArea.grid(row=0, column=0, sticky="nsew")
        self.scale.grid(row=0, column=1, sticky="nsew")
        self.vsb.grid(row=0, column=2, sticky="ns")
        self.hsb.grid(row=1, column=0, columnspan=2,sticky="ew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.graph_data()

    def graph_data(self):
        #self.DrawArea.create_line((0,0), (0,0), fill = "black")
        self.DrawArea.create_line((0,250), (1000,250), fill = "white")
        self.DrawArea.create_line((0,300), (2000,300), fill = "white")
        self.DrawArea.configure(scrollregion = self.DrawArea.bbox("all"))

root = tk.Tk()
Example(root).pack(fill="both", expand=True)

推荐答案

原因是因为这个声明:

self.DrawArea.configure(scrollregion=self.DrawArea.bbox("all"))

滚动区域定义了较大虚拟画布的哪些部分是可见的,或者可以通过将其滚动到视图中来使其可见.它给出了不一定以 0,0 为根的矩形的边界.

The scrollregion defined what part of the larger virtual canvas is visible, or can be made visible by scrolling it into view. It gives the bounds of a rectangle that doesn't necessarily have to be rooted at 0,0.

上面的代码行将获得画布上所有项目的边界框(bbox").边界框从最左上角的项目开始,在您的情况下为 0,250.创建虚拟点"时,边界框从 0,0* 开始.

The above line of code will get the bounding box ("bbox") of all of the items on the canvas. The bounding box starts with the upper-left-most item, which, in your case is at 0,250. When you create the "dummy point", the bounding box starts at 0,0*.

您可以通过在设置滚动区域后立即添加以下行来看到这一点:

You can see this by adding the following line right after setting the scrollregion:

print("the scrollregion is", self.DrawArea.cget("scrollregion"))

* 您可能会注意到滚动区域偏离了一两个像素.在我的机器上,它显示为 -2 248 2002 302.这是因为 bbox 方法不是 100% 精确的.为了性能,它有时会捏造一点.官方文档简单地说:

* You might notice that the scrollregion is off by a pixel or two. On my machine it shows it as -2 248 2002 302. This is because the bbox method isn't 100% precise. It will sometimes fudge a little for the sake of performance. The official documentation simply states:

返回值可能会高估实际边界框一些像素.

The return value may overestimate the actual bounding box by a few pixels.

如果您知道您的点将始终在 0,0 到 3000,3000 的范围内,您可以直接将滚动区域设置为该值.使用 bbox 的结果只是表示当前画布上的所有内容"的便捷快捷方式.

If you know your points will always be in range of 0,0 to, say, 3000,3000, you can directly set the scrollregion to that value. Using the result of bbox is just a handy shortcut to mean "everything currently on the canvas".

在您的情况下,您可以将该配置语句更改为如下所示:

In your case, you can change that configure statement to look like this:

self.DrawArea.configure(scrollregion=(0,0,3000,3000))

这篇关于Python tkinter 第一行未在正确的 y 位置绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆