我有以下Tkinter应用程序,该应用程序基本上可以通过在输入框中插入新值来帮助您配置新网络。用户输入的该值使用模板保存在文件中的某个位置。该脚本运行良好,但问题是我看到“租户”输入框比“网络名称”输入框高。他们的标签也是如此。然后,当我们单击添加网络并获得另一个2输入框时,将以非常奇怪的方式显示。
如何对齐标签+输入框,如何让它们分别显示在标签+输入框的每一组下?
类似:
租户[entrybox]网络[entrybox]
租户[entrybox]网络[entrybox]
..............................................
import Tkinter as tk
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.networks = []
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
start_label = tk.Label(self, text="This is the start page")
network_button = tk.Button(self, text="Create the networks", command=lambda: master.switch_frame(Networks))
start_label.pack(side="top", fill="x", pady=10)
deployment_button.pack()
network_button.pack()
subnet_button.pack()
neutronports_button.pack()
script_button.pack()
class Networks(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.entries=[]
self.create_widgets(master)
def create_widgets(self, master):
label = tk.Label(self, text="Insert the name of the networks")
start_button = tk.Button(self, text="Return to start page", command=lambda: master.switch_frame(StartPage))
new_network_button = tk.Button(self, text="Add network", command=self.add_network)
new_network_button.bind("<Return>", self.add_network)
new_network_button.grid(row=len(self.master.networks), column=3, padx=4, pady=6, sticky="W")
next_button=tk.Button(self, text="Submit", command=self.networks)
next_button.grid(row=1500, column=5,padx=4, pady=6, sticky="W")
label.pack(side="top", fill="x", pady=10)
start_button.pack()
new_network_button.pack()
next_button.pack()
for index, network in enumerate(self.master.networks):
self.render_network_field(network, index)
def add_network(self):
requirements={'variable': tk.StringVar(self.master), 'cloudvariable': tk.StringVar(self.master)}
if requirements:
self.master.networks.append(requirements)
self.master.switch_frame(Networks)
else:
tkMessageBox.showerror("tk", "No networks defined!")
self.master.switch_frame(Networks)
def render_network_field(self, network, index):
labelText=tk.StringVar()
labelText.set("Tenant name")
labelDir=tk.Label(self,textvariable=labelText, height=4)
labelDir.pack(side="top")
nw_entry_field = tk.Entry(self, textvariable=network['variable'])
nw_entry_field.grid(row=index, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")
nw_entry_field.pack(side="right")
labelText=tk.StringVar()
labelText.set("Network name")
labelDir=tk.Label(self,textvariable=labelText, height=4)
labelDir.pack(side="top")
cloud_entry_field = tk.Entry(self, textvariable=network['cloudvariable'])
cloud_entry_field.grid(row=index, column=0, columnspan=2, padx=4, pady=6, sticky="NEWS")
cloud_entry_field.pack(side="right")
self.entries.append((network['cloudvariable'],network['variable']))
def networks(self):
with open("/home/dante/networks.yml", "w") as f:
f.write("--- #" + "\n")
for ent, cloudent in self.entries:
network_value=ent.get()
cloud_value=cloudent.get()
if network_value:
if cloud_value:
f.write("- { cloud: "+cloud_value+", network: "+network_value+ " }"+ "\n")
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
答案 0 :(得分:0)
以下是我为解决您的问题而编写的简单代码。您只需要将函数register()放在您的类中,当您按下按钮寄存器(将它放在同一个类中)时便会执行该函数。
sh 'echo \'abc123 blahblahblah\' | nc intake.logs.datadoghq.com 10514'
编辑:
要解决您的错误,请将s设为全局变量。这将为文件中的所有代码块定义。另外,您可以为其设置一个新值,然后在其他任何地方使用该新值
import tkinter as tk
from tkinter import *
def register():
username = x1.get()
password = y1.get()
if username!="" and password=="":
file = open("accountfile.txt","a")
file.write(username)
file.write(" ")
file.write(password)
file.write("\n")
file.close()
root=Tk()
# String variables
x1=StringVar()
y1=StringVar()
Us=Entry(root,textvariable=x1)
Us.grid()
Ps=Entry(root, textvariable=y1)
Ps.grid()
Reg=Button(root,text="Register",command=lambda:register())
Reg.grid()
root.mainloop()
希望这会有所帮助