0
我有一個類名稱字段的端口。 然後我有一個主窗口,它打開一個表單來輸入新的端口。這種形式在第一行上有一個按鈕,當按下時,新增一個端口到框架,您可以在其中輸入新端口的名稱等。Python - Tkinter。爲什麼我的tkinter條目不能連接到列表中的最後一個變量?
輸入框的文本變量我已連接到上一個端口的名稱端口列表,但是當我按打印按鈕時,它不會打印正確的名稱。
如果轉產
MAIN_gui = MAIN_GUI(root)
到
MAIN_interface_pkg(root)
然後它的作品,因爲我想要的。
這是我第一次嘗試用Python和Tkinter的,所以請有一定的監督與壞的編碼...
#!/usr/bin/env python
import Tkinter as tk
MAIN_VERSION='3.6e'
class Port:
def __init__(self, name, mode, direction):
self.name = tk.StringVar()
self.name.set(name)
self.mode= tk.StringVar()
self.mode.set(mode)
self.direction= tk.StringVar()
self.direction.set(direction)
def quit():
root.destroy()
class MAIN_GUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self,parent)
frame = tk.Frame(parent)
frame.pack(expand='0', fill='x')
frame_buttons = tk.Frame(frame, borderwidth='1', relief='groove')
frame_buttons.pack(side=tk.TOP)
frame_quit = tk.Frame(frame, borderwidth='1', relief='groove')
frame_quit.pack(side=tk.TOP)
################
label = tk.Label(frame, text="MAIN Gui version "+MAIN_VERSION)
label.pack(side=tk.TOP)
################
quit_button = tk.Button(frame_quit, text="QUIT", fg="red", command=root.quit)
quit_button.pack(side=tk.TOP)
intf_button = tk.Button(frame_buttons, text="Create Interface Package", command=self.intf_button_action)
intf_button.pack(side=tk.LEFT)
def intf_button_action(self):
global MAIN_interface_pkg_window
if MAIN_interface_pkg_window is None:
MAIN_interface_pkg_window = tk.Tk()
MAIN_interface_pkg = MAIN_INTERFACE_PKG(MAIN_interface_pkg_window)
else:
MAIN_interface_pkg_window.update()
MAIN_interface_pkg_window.deiconify()
class MAIN_INTERFACE_PKG(tk.Frame):
def __init__(self, parent):
self.ports = []
self.main_frame = tk.Frame(parent)
self.main_frame.pack()
self.frame_button = tk.Frame(self.main_frame)
self.frame_button.pack(side=tk.BOTTOM)
self.print_button = tk.Button(self.frame_button, text="Print", fg="blue", command=self._dbg)
self.print_button.pack(side=tk.TOP)
self.frame_three = tk.Frame(self.main_frame, borderwidth=3, relief="groove")
self.frame_three.pack(side=tk.TOP)
self._addport(self.frame_three)
def _dbg(self):
for port in self.ports:
print("DBG:" + str(port.name.get()) + " - " + port.mode.get())
def _addport(self,frame):
tmp_port = Port("name", "Two", "Input")
self.ports.append(tmp_port)
self.frame_addport = tk.Frame(frame)
self.frame_addport.pack(side=tk.TOP)
if (len(self.ports) == 1):
self.button_portname = tk.Button(self.frame_addport, text = "[+] Port Name:", width=12, command=lambda:self._addport(frame))
self.button_portname.pack(side=tk.LEFT)
else:
self.label_portname = tk.Label(self.frame_addport, text = " Port Name:", width=14)
self.label_portname.pack(side=tk.LEFT, padx=4)
self.e = tk.Entry(self.frame_addport, textvariable=self.ports[-1].name)
self.e.pack(side=tk.LEFT)
for text in ["One", "Two", "Three", "Four"]:
self.b = tk.Radiobutton(self.frame_addport, text=text,
variable=self.ports[-1].mode, value=text)
self.b.pack(side=tk.LEFT)
root=tk.Tk()
MAIN_interface_pkg_window = tk.Tk()
MAIN_interface_pkg = MAIN_INTERFACE_PKG(MAIN_interface_pkg_window)
MAIN_interface_pkg_window.withdraw()
MAIN_gui= MAIN_GUI(root)
root.mainloop()