将 tkinter 画布和框架分开 [英] Splitting tkinter canvas and frame apart

查看:37
本文介绍了将 tkinter 画布和框架分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

左侧有一个 tkinter 框架,用于标签、复选按钮等.右侧是显示地图的画布.我可以滚动地图,它会给出鼠标指针在相关时间在地图上所处位置的经度/纬度坐标.我可以点击地图,它会放大地图.问题是当我在我想要显示底层地图数据的框架上时,当我在框架上滚动鼠标时,经度/纬度会发生变化,即使我不在画布上.如果我点击框架,还没有在那里放置任何复选按钮来测试它,它会像在画布上一样放大.

Got a tkinter frame on the left being used for labels, checkbuttons, etc. On the right is a canvas displaying a map. I can scroll over the map and it will give the longitude/latitude coordinates of where the mouse pointer is located on the map at the time in question. I can click on the map and it will zoom in on the map. The problem is when I'm on the frame where I want to display underlying map data as I scroll the mouse across the frame the longitude/latitude changes, even though I'm not on the canvas. If I click on the frame, haven't put any checkbuttons on there yet to test it that way, it zooms right in just like it would over on the canvas.

有什么办法可以将框架和画布的动作感知"分开,使它们分开.

Is there any way to split apart the action 'sensing' of the frame and canvas to keep them separate.

我会贴出代码,有点长,但我已经迟到了,所以我要离开这里了.

I would post up the code, a bit lengthy, but I got get out of here as I'm already running late.

我回来了,感谢 Bryan 的回复,我想我明白他要做什么,只是不知道该怎么做.在几次尝试中,似乎没有任何效果.当然,我仍然不能完全确定下面代码中的 (self,parent) 'addressing' 方法.

I'm back and thanks to Bryan's reply I think I understand what he was saying to do, just not sure how to do it. In a couple of attempts nothing seemed to work. Granted I'm still not fully sure of the (self,parent) 'addressing' method in the code below.

此外,我认为在不久的将来,需要能够将鼠标按钮分别引用到画布和框架的可能性很高,也就是根据我单击的位置执行不同的操作.幸运的是,由于不得不早点离开这里而延迟了,并且有了 Bryan 的回答,我已经能够进一步缩短代码,现在的代码正是我所说的.发布代码的延迟对我有利.

Also I see high probability coming up in the not to distant future of needing to be able to reference the mouse button to both t he canvas and the frame separately, aka have it do different things depending on where I have clicked on. Fortunately with the delay thanks to having to get out of here earlier and with Bryan's answer I have been able to shorten the code down even more and now have code that is doing exactly what I'm talking about. The delay in posting code worked to my benefit.

import tkinter as tk
from tkinter import *

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

        self.frame = tk.Frame(self,bg='black', width=1366, height=714)
        self.frame1 = tk.Frame(self,bg='gray', width=652, height=714)
        self.frame.pack()
        self.canvas = tk.Canvas(self, background="black", width=714, height=714)
        self.canvas.pack_propagate(0)
        self.canvas.place(x=652,y=0)
        self.frame1.pack_propagate(0)
        self.frame1.place(x=0,y=0)

        self.longitudecenter = -95.9477127
        self.latitudecenter = 36.989772
        self.p = 57.935628
        global v
        s = Canvas(self, width=150, height=20)
        s.pack_propagate(0)
        s.place(x=0,y=695)
        v = Label(s, bg='gray',fg='black',borderwidth=0,anchor='w')
        v.pack()

        parent.bind("<Motion>", self.on_motion)
        self.canvas.focus_set()

        self.canvas.configure(xscrollincrement=1, yscrollincrement=1)

   def on_motion(self, event):
        self.canvas.delete("sx")
        self.startx, self.starty = self.canvas.canvasx(event.x),self.canvas.canvasy(event.y)
        px = -(round((-self.longitudecenter + (self.p/2))- (self.startx * (self.p/714)),5))
        py = round((self.latitudecenter + (self.p/2))-(self.starty * (self.p /714)),5)
        v.config(text = "Longitude: " + str(px))

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

这是我一直在使用的一部分.如何更改它以便我可以分别绑定到框架和画布.现在我只需要在鼠标位置的情况下能够绑定到画布,但将来我将需要能够使用鼠标点击,甚至可能在画布和框架上分别使用鼠标位置.(谁知道这个项目自三周前开始以来发生了多少变化/进步......天空是极限).

This is part of what I've been using. How do I change it so I can bind to to the frame and to the canvas separately. Right now I only need, with the case of the mouse position, to be able to bind to the canvas, but in the future I will need to be able to use mouse clicks, maybe even mouse position separately on the canvas and frame.(who knows given how much this project has changed/advanced since I started it three weeks ago...the sky is the limit).

推荐答案

如果您希望绑定仅针对特定小部件触发,但要绑定到该小部件而不是包含的小部件.

If you want a binding to only fire for a specific widget, but the binding on that widget rather than on a containing widget.

改变这个:

parent.bind("<Motion>", self.on_motion)

为此:

self.canvas.bind("<Motion>", self.on_motion)

这篇关于将 tkinter 画布和框架分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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