在 tkinter 中制作“粘贴"窗口的函数

编程入门 行业动态 更新时间:2024-10-25 00:24:42
本文介绍了在 tkinter 中制作“粘贴"窗口的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

使用 tkinter 我想在我的程序中实现一个有趣的功能.单击我的 calendar.window 中代表月份日期的按钮,显示一个新窗口,任何人都可以在其中做笔记,并且必须粘贴"此窗口;到日历窗口.尝试拉取第一个或最后一个窗口是否必须导致拖动两者.错误拖拽的例子.如果没有那个功能也许有办法制作窗口出现在预定义位置并禁用拖动功能.

Using tkinter I want to implement a funny feature in my programm. Clicking on the button which represents date of the month in my calendar.window showing up a new window where anyone can make notes and this window must be "pasted" to calendar.window. Trying to pull whether the first or the last window must result in dragging both of them. Example of the wrong dragging. If there is no that function maybe there are means to make windows to appear in pre-defined position and disable dragging function.

主模块

import tkinter as tk
import re
from Calendar import show_calendar
from Notes_widget import show_notes_widget

class MainMenu(tk.Frame):
    def __init__(self, parent,):
        super().__init__(parent)
        self.init_main()

        # Configuring Frame object on the Main window
    def init_main(self):
        self.btn1 = tk.Button(text='Choose the date', width=20, height=3, command=show_calendar)
        self.btn2 = tk.Button(text='Enter your notes', width=20, height=3,
                              command=lambda : show_notes_widget(self.get_root_position()))

        self.btn1.pack()
        self.btn2.pack()

    def get_root_position(self):
        self.update_idletasks()
        self.width, self.height, self.x, self.y = re.split(r'[x+]', root.geometry())
        return self.width, self.height, self.x, self.y

if __name__ == '__main__':
    # Creating Main window
    root = tk.Tk()
    root.title('ИВРО')
    root.update_idletasks()
    root.width = 180
    root.height = 110
    root.x = (root.winfo_screenwidth() - root.winfo_reqwidth()) / 2
    root.y = (root.winfo_screenheight() - root.winfo_reqheight()) / 2
    root.geometry(f'{root.width}x{root.height}+{int(root.x - root.width / 2)}+{int(root.y - root.height / 2)}')
    root.resizable(False, False)

    # Creating Frame object
    app = MainMenu(root)
    app.pack()

    root.mainloop()

和第二个模块

import tkinter as tk

class NotesWidget(tk.Toplevel):
    def __init__(self, *args):
        tk.Toplevel.__init__(self)

        self.width, self.height, self.x, self.y = args
        self.init_child()

    def init_child(self):
        self.title("Введите заметку")

        self.text = tk.Text(self, height = 26, width = 35, bg="darkgreen", fg='white',
                            wrap='word', font=('Verdana', 10, 'bold'))
        self.text.pack()
        self.text.grid_propagate(True)
        self.btn = tk.Button(self, text='Добавить')
        self.btn.pack(ipady=10)


        self.geometry(f'300x465+{int(self.x) + int(self.width) + 12}+{int(self.y) - int(self.height)}')
        self.resizable(False, False)
        self.grab_set()
        self.focus_set()


def show_notes_widget(coordinates):
    width, height, x, y = coordinates
    notes_widget=NotesWidget(width, height, x, y)
    notes_widget.mainloop()

推荐答案

您要求的功能通常被称为停靠";而不是粘贴"

The feature you're asking for is more commonly referred to as "docked" rather than "pasted"

虽然 tkinter 不直接支持它,但它很容易模拟.Tkinter 提供了移动窗口的方法,以及知道何时移动窗口的方法.您需要做的就是检测主窗口何时移动,并移动另一个窗口以匹配新位置.

While tkinter doesn't directly support that, it's pretty easy to simulate. Tkinter provides methods for moving a window, and a way to know when a window is moved. All you need to do is detect when the main window moves, and move the other window to match the new location.

事件 将在窗口移动时触发(以及其他一些原因).向该事件添加绑定可以很好地将停靠窗口保持在正确的位置.

The event <Configure> will be triggered whenever a window is moved (and for a few other reasons). Adding a binding to that event works well to keep the docked window in the right location.

这是一个简单的例子.当主窗口移动时,第二个会滞后,但我认为这是不可避免的.

Here's a simple example. The second will lag behind when the main window is moved, but I think that's unavoidable.

import tkinter as tk
import re

root = tk.Tk()
other = tk.Toplevel(root)

def dock(child, master):
    width, height, x, y = re.split(r'[x+]', master.geometry())
    x = int(x) + master.winfo_width()
    child.geometry(f"+{x}+{y}")

root.bind("<Configure>", lambda event: dock(other, root))
root.mainloop()

这篇关于在 tkinter 中制作“粘贴"窗口的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:05:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   窗口   tkinter   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!