有关Pysimplegui TK版本中所需功能的查询

发布于 2025-02-12 09:38:09 字数 190 浏览 0 评论 0原文

我已经使用图形元素构建了一个面向图形的软件包。我需要根据图元坐标进行键盘输入。目前,我正在使用键盘中的事件使用draw_text将字符放在图形元素上。它起作用,但有点慢,我会遇到问题,可以解释我从不同平台中获得的密钥代码,而我将回声重新回到图形元素的开销无济于事。

我的问题。在Pysimplegui(TK)中,是否可以直接在图坐标上使用TK条目函数?

I have built a graphic oriented package using the Graph element. I need to do keyboard input based on Graph Element coordinates. Currently I am using the events that come in from the keyboard to place characters on the Graph element using draw_text. It works but is a bit slow and I get into problems with interpreting the key codes I get back from different platforms and the overhead with me doing the echoing back on to the Graph element does not help.

My Question. In PySimpleGui(Tk) is there a way to use the Tk Entry function directly on Graph Coordinates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2025-02-19 09:38:09

IMO,可以像您的要求一样完成,但要复杂得多。

这里只是一种在图元素上输入文本的简单方法。

import PySimpleGUI as sg

font = ('Courier New', 16, 'bold')

layout = [
    [sg.Input(expand_x=True, key='INPUT')],
    [sg.Graph((320, 240), (0, 0), (320, 240), enable_events=True, key='GRAPH',
        background_color='green')],
]
window = sg.Window('Draw Text', layout, margins=(0, 0), finalize=True)
entry, graph = window['INPUT'], window['GRAPH']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'GRAPH':
        location, text = values['GRAPH'], values['INPUT']
        if text:
            graph.draw_text(text, location, font=font, color='white', text_location=sg.TEXT_LOCATION_LEFT)

window.close()

在这里,类似于您的请求,但需要复杂且需要进行TKINTER代码。

import PySimpleGUI as sg

def entry_callback(event, graph, entry_id, font, location, color):
    widget = event.widget
    text = widget.get()
    graph.widget.delete(entry_id)
    if text:
        graph.draw_text(text, location=location, font=font, color=color, text_location='sw')

font = ('Courier New', 16, 'bold')

layout = [
    [sg.Graph((320, 240), (0, 0), (320, 240), enable_events=True, key='GRAPH',
        background_color='green')],
]
window = sg.Window('Draw Text', layout, margins=(0, 0), finalize=True)
graph = window['GRAPH']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'GRAPH':
        location = tuple(map(int, graph._convert_xy_to_canvas_xy(*values['GRAPH'])))
        entry = sg.tk.Entry(graph.widget, font=font, fg='white', bg='green', width=45)
        entry_id = graph.widget.create_window(*location, window=entry, anchor="sw")
        entry.bind('<Return>', lambda event, graph=graph, entry_id=entry_id, font=font, location=values['GRAPH'], color='white':entry_callback(event, graph, entry_id, font, location, color))
        entry.focus_force()

window.close()

IMO, it can be done like your request, but much complex.

Here only a simple way to enter text on a Graph element.

import PySimpleGUI as sg

font = ('Courier New', 16, 'bold')

layout = [
    [sg.Input(expand_x=True, key='INPUT')],
    [sg.Graph((320, 240), (0, 0), (320, 240), enable_events=True, key='GRAPH',
        background_color='green')],
]
window = sg.Window('Draw Text', layout, margins=(0, 0), finalize=True)
entry, graph = window['INPUT'], window['GRAPH']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'GRAPH':
        location, text = values['GRAPH'], values['INPUT']
        if text:
            graph.draw_text(text, location, font=font, color='white', text_location=sg.TEXT_LOCATION_LEFT)

window.close()

enter image description here

Here, something like your request, but much complex and tkinter code required.

import PySimpleGUI as sg

def entry_callback(event, graph, entry_id, font, location, color):
    widget = event.widget
    text = widget.get()
    graph.widget.delete(entry_id)
    if text:
        graph.draw_text(text, location=location, font=font, color=color, text_location='sw')

font = ('Courier New', 16, 'bold')

layout = [
    [sg.Graph((320, 240), (0, 0), (320, 240), enable_events=True, key='GRAPH',
        background_color='green')],
]
window = sg.Window('Draw Text', layout, margins=(0, 0), finalize=True)
graph = window['GRAPH']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'GRAPH':
        location = tuple(map(int, graph._convert_xy_to_canvas_xy(*values['GRAPH'])))
        entry = sg.tk.Entry(graph.widget, font=font, fg='white', bg='green', width=45)
        entry_id = graph.widget.create_window(*location, window=entry, anchor="sw")
        entry.bind('<Return>', lambda event, graph=graph, entry_id=entry_id, font=font, location=values['GRAPH'], color='white':entry_callback(event, graph, entry_id, font, location, color))
        entry.focus_force()

window.close()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文