python-3.x 如何从一组入口小部件中指定光标所在的小部件

5uzkadbs  于 2022-12-24  发布在  Python
关注(0)|答案(1)|浏览(167)

假设我有几个入口小部件(来自tkinter),我如何获得一个索引或焦点在我选择的入口上?主要任务是我有一个图片,当我点击它时,我可以从中获取坐标,我需要这些坐标立即输入到字段中。

import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image
from PIL import ImageTk

class interface:

    def __init__(self):
        self.H = None
        self.panelA = None
        self.path = None
        self.s = None
        self.a = None
        option_list = ["Агрегат №8"]
        self.root = tk.Tk()

        # Первый FRAME
        self.f1 = tk.Frame(self.root)
        self.f1.grid(row=0, column=0)

        # Второй FRAME
        self.f2 = tk.Frame(self.root)
        self.f2.grid(row=0, column=1, rowspan=2)

        # Третий FRAME
        self.f3 = tk.Frame(self.root)
        self.f3.grid(row=0, column=2, rowspan=2)

        # Четвертый FRAME
        self.f4 = tk.Frame(self.root)
        self.f4.grid(row=2, columnspan=3, sticky='ew')

        # Пятый FRAME
        self.f5 = tk.Frame(self.root)
        self.f5.grid(row=1, sticky='ew',)


        # Entry
        self.e_H = tk.Entry(self.f1)
        self.e_H.grid(column=1, row=1, sticky='ew',)

        # Label 1 АГРЕГАТ
        self.l1 = tk.Label(self.f1, text="Выберите агрегат")
        self.l1.grid(column=0, row=0, sticky='w')

        # Label 2 НАПОР
        self.l2 = tk.Label(self.f1, text="Введите напор H, м")
        self.l2.grid(column=0, row=1, sticky='w')

        # Label 3
        self.l3 = tk.Label(self.f5, text="Укажите значения мощности соот-му КПД")
        self.l3.grid(column=0, row=2, sticky='ew', columnspan=4)

        # Label 4
        self.l4 = tk.Label(self.f3, text="Рассчитанные значения")
        self.l4.grid(column=0, row=0, sticky='ew', columnspan=3)

        # Button АГРЕГАТ
        b1 = tk.Button(self.f1, text="Выбрать", command=self.select_image)
        b1.grid(column=4, row=0, sticky='ew')

        # Button РАСЧЕТ
        b2 = tk.Button(self.f5, text="Рассчитать")
        b2.grid(column=0, row=17, sticky='ew', columnspan=4)

        self.canvas = tk.Canvas(self.f2, height=490, width=1100)
        self.canvas.grid(column=1, row=0)

        self.canvas = tk.Canvas(self.f4, height=450, width=1920)
        self.canvas.grid(column=0, row=0)

        self.comboExample = ttk.Combobox(self.f1, values=option_list)
        self.comboExample.grid(column=1, row=0, sticky='w')

        # Button НАПОР
        b3 = tk.Button(self.f1, text="Задать напор", command=self.pressure)
        b3.grid(column=4, row=1, sticky='w')

        # Table 2
        columns1 = ['N', 'ny']
        self.table2 = ttk.Treeview(self.f3, columns=columns1, show='headings', height=24)
        self.table2.heading("N", text="Мощность, МВт")
        self.table2.heading("ny", text="КПД, %")
        self.table2.grid(column=0, row=1, sticky='ew')

        self.table_entry()
        self.root.mainloop()

    def get_s(self):
        return self.s

    def select_image(self):
        try:
            self.selection = self.comboExample.get()
            path_Ag = {"Агрегат №8": "Graf/Ag8.png"}
            image = Image.open(path_Ag[self.selection])
            resize_image = image.resize((1100, 519))
            image = ImageTk.PhotoImage(resize_image)

            if self.panelA is None:
                self.panelA = tk.Label(self.f2, image=image)
                self.panelA.bind("<Button-1>", self.callback)
                self.panelA.image = image
                self.panelA.grid(column=1, row=0)

            else:
                self.panelA.configure(image=image)
                self.panelA.image = image
                self.panelA.bind("<Button-1>", self.callback)

        except KeyError:
            messagebox.showerror("Ошибка", "Выберете агрегат")

    def callback(self, event):
        if self.selection == "Агрегат №8":
            self.num = str(event.x * 118)
            self.list_e[0].delete(0, 'end')
            self.list_e[0].insert(0, self.num)

    def table_entry(self):
        self.list_e = []
        count_list_e = 0
        c = 0
        d = 1
        for i in range(2):
            for j in range(11 - i):
                self.list_e.append(tk.Entry(self.f5, width=10, fg='blue', font=('Arial', 16, 'bold')))
                self.list_e[count_list_e].grid(row=j + 3, column=i + d, sticky='ew')
                tk.Label(self.f5, text=f"{70 + count_list_e}%", width=11).grid(column=i + c, row=3 + j,)
                count_list_e += 1
            c = 1
            d = 2

    def pressure(self):
        try:
            int(self.e_H.get())
            self.H = self.e_H.get()
        except ValueError:
            messagebox.showerror("Ошибка", "Введите численное значение")

obj_interface = interface()

'
我试着绑定,但是效果不太好,可能是我做错了

o2gm4chl

o2gm4chl1#

您可以在任何小部件上调用focus_get方法来获取对具有键盘焦点的小部件的引用,因为始终只有一个小部件具有键盘焦点。

相关问题