当时打算分两个类完成,WinGUI写布局,Win写事件什么的。
class WinGUI(Tk):
def __init__(self):
super().__init__()
self.__win()
self.label_frame_feature = Frame_feature(self)
self.button_calcNow = self.__button_calcNow()
def __win(self):
self.title("Heat Loss Calculator")
width = 550
height = 540
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.geometry(geometry)
self.resizable(width=True, height=True)
class Frame_feature(LabelFrame):
def __init__(self,parent):
super().__init__(parent)
self.__frame()
self.select_box_level = self.select_box_level()
def select_box_level(self):
self.var = StringVar()
self.cb = Combobox(self, state="readonly",textvariable=self.var)
self.cb['values'] = ("Ground floor","Intermediated level","Top floor")
self.cb.place(x=10, y=40, width=150, height=24)
return self.cb, self.var.get() #这个self.var.get()就是我想后面用的
class Frame_result(Frame):
def __init__(self,parent):
super().__init__(parent)
self.__frame()
self.input_hLoss = self.input_hLoss()
def input_hLoss(self):
ipt = Entry(self)
ipt.place(x=120, y=30, width=81, height=24)
return ipt
这是另一个Win类
from tkinter import *
from tkinter.ttk import *
from tkinter_helper import WinGUI
class Win(WinGUI):
def __init__(self):
super().__init__()
self.__event_bind()
def slc_get_level(self,evt):
print(self.level)
def __event_bind(self):
self.button_calcNow.bind('' ,self.slc_get_level)
if __name__ == "__main__":
win = Win()
win.mainloop()
self.level_get = Frame_feature(self).select_box_level[1]
这是我后来加在WinGUI 的init下面的
level那个下拉框中的值可以出现在 Frame_result中的输入框中
回答不易,回答有用请采纳!
在 Python 中,你可以在一个类中创建一个实例变量,在另一个类中访问它。例如,在 WinGUI 类中,你可以创建一个实例变量来存储 entry 的值,然后在 Win 类中访问它。
WinGUI 类:
class WinGUI:
def __init__(self):
self.entry_value = ""
def get_entry_value(self):
return self.entry_value
def set_entry_value(self, value):
self.entry_value = value
Win 类:
class Win:
def __init__(self, win_gui_instance):
self.win_gui = win_gui_instance
def update_entry_value(self, new_value):
self.win_gui.set_entry_value(new_value)
在你的主程序中,可以创建WinGUI 和Win 类的实例,并将WinGUI的实例传入Win的实例中。
win_gui = WinGUI()
win = Win(win_gui)
然后你可以在win类中调用 update_entry_value() 来更新 WinGUI 类中的 entry_value 变量的值,并在 WinGUI 类中调用 get_entry_value() 方法来获取 entry_value 的值。总之,可以使用一种叫做"传递实例"的方法,在一个类中访问另一个类中的变量。
相关文章:https://blog.csdn.net/weixin_39716088/article/details/112963552?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167465986516800192246387%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167465986516800192246387&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-9-112963552-null-null.142^v71^wechat,201^v4^add_ask&utm_term=python%E4%BC%A0%E9%80%92%E5%AE%9E%E4%BE%8B&spm=1018.2226.3001.4449
新年快乐🎉,望采纳,谢谢!
可以使用参数传递的方式来实现。
例如:
class A():
def __init__(self):
self.value = 0
class B(A):
def __init__(self, value):
super().__init__()
self.value = value
a = A()
b = B(a.value)
在类B中可以使用参数value来传递类A中获得的值。
问题分析:
根据您描述的问题和现象,有两个类,需要在一个类里面显示另外一个类里面的数据,这个问题的本质,其实就是python中类与类之间数据传递参数的问题。具体的解决方法,给你提供一个思路,其实不难理解:
解决方法:
你可以把另一个类,比如类A,的对象以参数的形式传入到类B中,可以在B的构造方法中传递进去或者在B中专门写一个方法把A的对象传递进去,然后就可以在B中使用A的对象访问A中的数据了。
代码实现:
Class B():
def __init__(self,A a):
self.a =a
def setA(self,A a):
self.a =a
然后就可以在b中通过a调用a里面的数据
不知道你这个问题是否已经解决, 如果还没有解决的话: