我有一个引擎模块,其中包含几个辅助方法和属性:
引擎.py
class World:
def __init__(self):
# stuff
def foo(a, b):
c = a + b # trivial example
return c
Run Code Online (Sandbox Code Playgroud)
然后我有一个 main.py,其中包含 UI 文件(在 QtDesigner 中创建并使用我拥有的脚本转换为 .py):
主要.py
from PyQt4 import QtGui
import design1
import design2
import engine
class MainWindow(QtGui.QMainWindow, design1.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.world = engine.World()
self.new_window_button.clicked.connect(self.open_new_window)
def open_new_window(self):
self.window_to_open = ChildWindow(self)
self.window_to_open.show()
class ChildWindow(QtGui.QMainWindow, design2.Ui_MainWindow):
def __init__(self, parent):
super(ChildWindow, self).__init__(parent)
self.setupUi(self)
print(self.world.foo(1, 2)) # trivial example
def main():
app = QtGui.QApplication(sys.argv)
window = MainWindow() …Run Code Online (Sandbox Code Playgroud) 我想在python中制作一个简单的温度转换计算器.我想要做的是能够输入一个数字,并让另一方自动更新,而无需按下按钮.现在我只能朝一个方向努力.我可以编码它,以便它可以从F到C,或从C到F.但不是两种方式.
显然after不是要走的路.我需要某种onUpdate东西.TIA!
import Tkinter as tk
root = tk.Tk()
temp_f_number = tk.DoubleVar()
temp_c_number = tk.DoubleVar()
tk.Label(root, text="F").grid(row=0, column=0)
tk.Label(root, text="C").grid(row=0, column=1)
temp_f = tk.Entry(root, textvariable=temp_f_number)
temp_c = tk.Entry(root, textvariable=temp_c_number)
temp_f.grid(row=1, column=0)
temp_c.grid(row=1, column=1)
def update():
temp_f_float = float(temp_f.get())
temp_c_float = float(temp_c.get())
new_temp_c = round((temp_f_float - 32) * (5 / float(9)), 2)
new_temp_f = round((temp_c_float * (9 / float(5)) + 32), 2)
temp_c.delete(0, tk.END)
temp_c.insert(0, new_temp_c)
temp_f.delete(0, tk.END)
temp_f.insert(0, new_temp_f)
root.after(2000, update)
root.after(1, update)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)