Python tkinter滚动条/图形比例减慢滚动 [英] Python tkinter scrollbar/graph scale slowing down scrolling

查看:31
本文介绍了Python tkinter滚动条/图形比例减慢滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的数据图表上放置一个滚动条.我把它放在那里,它滚动数据,但它也在屏幕右侧滚动比例(数据值).今天早上我一直在玩弄创建两个独立窗口的想法,一个用于数据图,一个用于比例.与您通常看到的相比,它看起来很不寻常,但是当我这样做时,我确实注意到一件事.使用数据图上的刻度,一个 gui,只要刻度仍然保留在屏幕上,滚动就会非常慢.一旦刻度移出屏幕,滚动速度就会达到我通常期望的速度.当我将比例尺移动到完全独立的 gui 时,滚动速度始终保持良好.我该如何克服这个问题?

I have been toying around with putting a scrollbar on my data graph. I have it on there and its scrolling the data but its also scrolling the scale(data values) on the right hand side of the screen. I've have toyed around this morning with the idea of creating two separate windows, one for the data graph and one for the scale. It looks rather unusual compared to what you normally see but I do notice one thing in particular when I do this. With the scale on the data graph, one gui, the scrolling is very slow as long as the scale still remains on the screen. Once the scale moves off the screen the scrolling speed picks up to what I would normally expect. When I move the scale to a completely separate gui the scrolling speed is consistently fine all the time. How do I overcome this problem?

我不确定为什么缩放会对滚动速度产生任何影响.无非是:

I'm not sure why the scale is having any kind of effect on the scrolling speed. It's nothing more than:

self.DrawArea.create_line((1298, 12), (1300, 12), fill = "white")
self.DrawArea.create_line((1290, 25), (1300, 25), fill = "white")
self.DrawArea.create_line((1298, 37), (1300, 37), fill = "white")
self.DrawArea.create_text((1320, 25), text = "5.0", fill = 'white')
self.DrawArea.create_text((1320, 50), text = "4.5", fill = 'white')
self.DrawArea.create_text((1320, 75), text = "4.0", fill = 'white')

沿着屏幕向下(是的,每 0.125 标记一次 5 到 -5...每 0.5 标记一次).

going down the screen(yes 5 to -5 marked out every every .125...labelled once every .5).

将比例尺和图形数据放在同一个gui上,并且仍然保持滚动速度是可行的.我没有更改字体,无论是大小还是类型,因为我不知道如何更改,因为 tkinter 文档中没有真正指出任何内容.

It is feasible to have the scale and the graph data on the same gui and still keep the scrolling speed. I haven't changed the font, either size or type as I'm not sure how to since nothing is really indicated in the tkinter documentation.

还有一种方法可以限制图形数据的显示位置.使用一个 gui,我将图形设置为 1350x615(600,底部 15 是滚动条).1300应该是显示数据,另外50是比例.现在我遇到的问题是数据在比例尺下绘制(比例尺显然放在最后).有什么办法可以限制它,以便数据只显示 0-1300,而比例显示 1301-1350?今天早上我也一直在玩 Frames,但到目前为止我还没有解决这个问题.

Also is there a way that I can limit where the graph data gets displayed. With one gui, I have the graph setup for 1350x615(600 with the bottom 15 being the scrollbar). 1300 should be the display data with the other 50 being the scale. Right now I have the issue that the data gets graphed underneath the scale(scale is obviously put on last). Is there any way I can limit it so the data only gets shown 0-1300 while the scale gets display 1301-1350? I've been toying around with Frames as well this morning but I have had no luck thus far at resolving this issue.

当我尝试使用键盘进行滚动时,我使用了 .move() 命令,但是当我改为使用滚动条时,我根本没有使用键盘,只是使用滚动条.当我在同一个 gui 上同时拥有图形和比例时,只要比例尺在屏幕上(尚未滚动关闭),图形在屏幕上的移动速度就会非常缓慢.一旦它离开屏幕,速度就会加快并移动,就好像我根本没有屏幕上的比例尺一样.当我用两个单独的窗口进行测试时也是如此.主图上的刻度会减慢滚动速度.

Edited: When I was trying to use the keyboard for scrolling I was using the .move() command but when I went to change to using the scrollbar I wasn't using the keyboard at all and just using the scrollbar. When I have both the graph and scale on the same gui as long as the scale is on the screen(hasn't been scrolled off yet) the graph moves very slowly across the screen. Once it's off the screen the pace picks up and moves as though I didn't have the scale on the screen at all. It's the same way when I test with two separate windows. The scale on the main graph slows the scrolling down.

将比例移动到另一个 gui 仍然无助于加载速度或显示图形的放大/缩小速度.

Moving the scale to another gui still doesn't help the load speed or the zoom in/out speed for displaying the graph though.

推荐答案

如果您想使用滚动条滚动而不是缩放滚动,您可能应该使用两个单独的窗口.我不确定您所说的与您通常看到的情况相比不寻常"是什么意思.如果您将两个画布并排放置,它们之间没有空间且具有相同的背景颜色,则用户将无法知道您正在同时使用两个画布小部件.

If you want to scroll with the scrollbar and not have the scale scroll, you should probably use two separate windows. I'm not sure what you mean by "unusual compared to what you normally see". If you put the two canvases side-by-side with no space between them and with the same background color, the user would have no way of knowing you're using two canvas widgets at the same time.

tkinter 画布可以在开始变得缓慢之前滚动数千个项目,因此很难说没有看到您的实际代码.

The tkinter canvas can scroll several thousand items before it starts to get sluggish, so it's hard to say without seeing your actual code.

这是一个绘制 10,000 个点的示例,比例尺位于单独的画布中:

here's an example that plots 10,000 points, with the scale in a separate canvas:

import Tkinter as tk
from random import randrange

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

        self.DrawArea = tk.Canvas(self, width=1000, 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.draw_scale()
        self.draw_points()

    def draw_scale(self):
        value = 5.0
        for y in range(25, 600, 25):
            self.scale.create_text((25, y), text=str(value), fill="white", anchor="ne")
            value -= 0.5

    def draw_points(self):
        import math
        for x in range(5,10000):
            y = randrange(600)
            color = "green" if (300 > y > 200) else "red"
            self.DrawArea.create_rectangle(x-2,y-2,x+2,y+2, fill=color)
        self.DrawArea.configure(scrollregion = self.DrawArea.bbox("all"))

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

这篇关于Python tkinter滚动条/图形比例减慢滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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