我试着让它这样当我按下回车键时,它会调用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()
Gtk.Window.set_default的文档告诉您如何设置默认情况下获得焦点的小部件。当焦点在一个按钮上时按enter只会触发该按钮的
clicked
信号。在从鼠标或键盘被激活的方式是相同的,并且是透明的,所以你已经把你的信号连接到它上面,就没有什么特别的事情要做了。在
假设您想在GtkEntry中按
enter
来计算新的数字:我改变了你的代码。在
相关问题 更多 >
编程相关推荐