按“回车”键调用gtk3中的函数

2024-09-30 18:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让它这样当我按下回车键时,它会调用click()函数,或者启动calculateButton的按键,我为我可怕的代码道歉,我是自学成才的,所以它在任何方面都不整洁或正确,但它是有效的。。。在

代码:

import gi
import math
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TableWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="Injection Time Calculator")

        table = Gtk.Table(7, 2, True)
        self.add(table)

        sizeLabel = Gtk.Label("WALL SIZE")
        widthLabel = Gtk.Label("WIDTH")
        heightLabel = Gtk.Label("HEIGHT")
        fsLabel = Gtk.Label("FLAT STUD?")
        ecLabel = Gtk.Label("EC?")
        injLabel = Gtk.Label()

        heightEntry=Gtk.Entry()
        widthEntry=Gtk.Entry()

        sizeBox = Gtk.ComboBoxText()

        sizeBox.append("1","90")
        sizeBox.append("2","140")
        sizeBox.append("3","219")

        fsCheckButton=Gtk.CheckButton()
        ecCheckButton=Gtk.CheckButton()

        calculateButton=Gtk.Button.new_with_label("CALCULATE INJECTION TIME")
        calculateButton.connect("clicked", self.Click)


        table.attach(sizeLabel, 0, 1, 0, 1)
        table.attach(widthLabel, 0, 1, 1, 2)
        table.attach(heightLabel, 0, 1, 2, 3)
        table.attach(fsLabel, 0, 1, 3, 4)
        table.attach(injLabel, 0, 2, 6, 7)
        table.attach(ecLabel, 0, 1, 4, 5)

        table.attach(calculateButton, 0, 2, 5, 6)

        table.attach(widthEntry, 1, 2, 1, 2)
        table.attach(heightEntry, 1, 2, 2, 3)

        table.attach(sizeBox, 1, 2, 0, 1)

        table.attach(fsCheckButton, 1, 2, 3, 4)
        table.attach(ecCheckButton, 1, 2, 4, 5)

        global sizeLabel
        global heightEntry
        global widthEntry
        global fsCheckButton
        global ecCheckButton
        global sizeBox
        global injLabel


    def Click(self, calculateButton):

        studWidth=sizeBox.get_active_text()

        areaHeight=heightEntry.get_text()
        areaWidth=widthEntry.get_text()

        if fsCheckButton.get_active():
            if studWidth=="90":
                areaWidth=float(areaWidth)-59
            elif studWidth=="140":
                areaWidth=float(areaWidth)-38
            elif studWidth=="219":
                areaWidth=float(areaWidth)-24.3

        if ecCheckButton.get_active():
            if studWidth=="90":
                areaWidth=float(areaWidth)-2*(59)
            elif studWidth=="140":
                areaWidth=float(areaWidth)-2*38
            elif studWidth=="219":
                areaWidth=float(areaWidth)-2*24.3

        timeVar=float(studWidth)*10.7/140
        injTime=((float(areaWidth)*float(areaHeight)*float(timeVar))/1000000)
        a= str( 'INJECTION TIME IS '+str(injTime)[:7]+' SECONDS OR '+str((math.ceil(injTime*5))/5)+' ROUNDED')
        injLabel.set_text(a)

        fsCheckButton.set_active(False)
        ecCheckButton.set_active(False)

win = TableWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Tags: selfgtkgettablefloatgloballabelactive
2条回答

Gtk.Window.set_default的文档告诉您如何设置默认情况下获得焦点的小部件。当焦点在一个按钮上时按enter只会触发该按钮的clicked信号。在

从鼠标或键盘被激活的方式是相同的,并且是透明的,所以你已经把你的信号连接到它上面,就没有什么特别的事情要做了。在

假设您想在GtkEntry中按enter来计算新的数字:

import gi
import math
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TableWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="Injection Time Calculator")

        table = Gtk.Table(7, 2, True)
        self.add(table)

        self.sizeLabel = Gtk.Label("WALL SIZE")
        widthLabel = Gtk.Label("WIDTH")
        heightLabel = Gtk.Label("HEIGHT")
        fsLabel = Gtk.Label("FLAT STUD?")
        ecLabel = Gtk.Label("EC?")
        self.injLabel = Gtk.Label()

        self.heightEntry=Gtk.Entry()
        self.heightEntry.connect("activate", self.calculate)
        self.widthEntry=Gtk.Entry()
        self.widthEntry.connect("activate", self.calculate)

        self.sizeBox = Gtk.ComboBoxText()

        self.sizeBox.append("1","90")
        self.sizeBox.append("2","140")
        self.sizeBox.append("3","219")

        self.fsCheckButton=Gtk.CheckButton()
        self.ecCheckButton=Gtk.CheckButton()

        calculateButton=Gtk.Button.new_with_label("CALCULATE INJECTION TIME")
        calculateButton.connect("clicked", self.calculate)


        table.attach(self.sizeLabel, 0, 1, 0, 1)
        table.attach(widthLabel, 0, 1, 1, 2)
        table.attach(heightLabel, 0, 1, 2, 3)
        table.attach(fsLabel, 0, 1, 3, 4)
        table.attach(self.injLabel, 0, 2, 6, 7)
        table.attach(ecLabel, 0, 1, 4, 5)

        table.attach(calculateButton, 0, 2, 5, 6)

        table.attach(self.widthEntry, 1, 2, 1, 2)
        table.attach(self.heightEntry, 1, 2, 2, 3)

        table.attach(self.sizeBox, 1, 2, 0, 1)

        table.attach(self.fsCheckButton, 1, 2, 3, 4)
        table.attach(self.ecCheckButton, 1, 2, 4, 5)


    def calculate (self, widget):
        studWidth=self.sizeBox.get_active_text()

        areaHeight=self.heightEntry.get_text()
        areaWidth=self.widthEntry.get_text()

        if self.fsCheckButton.get_active():
            if studWidth=="90":
                areaWidth=float(areaWidth)-59
            elif studWidth=="140":
                areaWidth=float(areaWidth)-38
            elif studWidth=="219":
                areaWidth=float(areaWidth)-24.3

        if self.ecCheckButton.get_active():
            if studWidth=="90":
                areaWidth=float(areaWidth)-2*(59)
            elif studWidth=="140":
                areaWidth=float(areaWidth)-2*38
            elif studWidth=="219":
                areaWidth=float(areaWidth)-2*24.3

        timeVar=float(studWidth)*10.7/140
        injTime=((float(areaWidth)*float(areaHeight)*float(timeVar))/1000000)
        a= str( 'INJECTION TIME IS '+str(injTime)[:7]+' SECONDS OR '+str((math.ceil(injTime*5))/5)+' ROUNDED')
        self.injLabel.set_text(a)

        self.fsCheckButton.set_active(False)
        self.ecCheckButton.set_active(False)

win = TableWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

我改变了你的代码。在

相关问题 更多 >