Python Tkinter在循环中运行,在变量中定义了x次

时间:2018-08-19 09:13:12

标签: python loops tkinter tkinter-entry

我想使用Tkinter创建一个Python脚本,该脚本定义网络的数量,并使用用户在输入字段中插入的值在下一页创建x定义网络。然后,用户插入的值将创建具有以下模式的文件:

--- #
networks:
- { cloud: cloud1, network:  }
- { cloud: cloud1, network:  }
- { cloud: cloud1, network:  }
- { cloud: cloud1, network:  }
...

示例:

如果用户在“定义网络数量”页面上输入20,那么我们应该在“定义网络”页面上具有20个输入字段,并在文件- { cloud: cloud1, network: }中有20行: >

@AXK非常感谢我的帮助。例如,当我要介绍10个以上的网络时,我也想滚动一下。我已尝试对AXK代码进行如下更改:

import Tkinter as tk
import tkFont as tkfont
import sys
from Tkinter import *

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(
            family="Helvetica", size=18, weight="bold", slant="italic"
        )
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.container = container
        self.current_frame = None

        self.num_networks = tk.IntVar()  # Var shared between pages
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        """Show a frame for the given page name"""
        if self.current_frame:
            self.current_frame.destroy()
            self.current_frame = None

        frame_class = globals()[page_name]  # Ugly, but works.
        frame = frame_class(parent=self.container, controller=self)
        frame.grid(row=0, column=0, sticky="nsew")
        frame.tkraise()
        self.current_frame = frame


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self,text="This is the start page",font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self,text="Define the number of networks",command=lambda: controller.show_frame("PageOne"))
        button1.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self,text="Define the number of networks",font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        nwnum = tk.Entry(self, textvariable=controller.num_networks)
        nwnum.pack()

        button1 = tk.Button(self,text="Go to the start page",command=lambda: controller.show_frame("StartPage"),)
        button1.pack()

        button2 = tk.Button(self,text="Define the networks",command=lambda: controller.show_frame("PageTwo"),)
        button2.pack()


class PageTwo(tk.Frame):
    def getent1(self):
      with open("/home/dante/output.txt", "w") as f:
        f.write("--- #" + "\n")
        f.write("networks:" + "\n")
        for entvar in self.entry_vars:
            value = entvar.get()
            if value:
                f.write("- { cloud: cloud1, network: "+ value+ " }"+ "\n")

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.scrollbar = Scrollbar(self, orient='vertical')
        label = tk.Label(self,text="Define the networks",font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        self.entries = []
        self.entry_vars = []

        self.scrollbar.config(command=self.yview)
        self.scrollbar.pack(side='right', fill='y')
        for t in range(1, self.controller.num_networks.get()+1):
            entvar = tk.StringVar()
            ent = tk.Entry(self, textvariable=entvar)
            self.entry_vars.append(entvar)
            self.entries.append(ent)
            ent.pack()
        button3 = tk.Button(self,text="Go to the start page",command=lambda: controller.show_frame("StartPage"),)
        button3.pack()
        button4 = tk.Button(self, text="Create the networks", command=self.getent1)
        button4.pack()

    def yview(self, *args):
        self.entry_vars.yview(*args)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

当我运行它时,我在选项卡的右侧看到滚动条,但是当我按下或向上时,出现以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "program.py", line 96, in yview
    self.entry_vars.yview(*args)
AttributeError: 'list' object has no attribute 'yview'

我该如何完成?

P.S。我对编程很陌生

1 个答案:

答案 0 :(得分:1)

我重构了一些东西:

  • 我更改了导入以与Python 3安装一起使用。您可能会有所不同。
  • 由于我没有sys.stdout,YAML输出被写入/home/dante,而不是文件。 ;)
  • 现在还没有立即初始化所有框架对象。相反,它们只是被创建 当show_frame被调用时。
  • 保存网络数量的Tk变量现在由controller拥有;子帧从那里写/读它。
  • 保存第3帧的网络名称的字符串变量存储在第3帧的列表中,并在YAML编写函数中从那里读取。

import tkinter as tk
from tkinter import font as tkfont
import sys


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(
            family="Helvetica", size=18, weight="bold", slant="italic"
        )
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.container = container
        self.current_frame = None

        self.num_networks = tk.IntVar()  # Var shared between pages
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        """Show a frame for the given page name"""
        if self.current_frame:
            self.current_frame.destroy()
            self.current_frame = None

        frame_class = globals()[page_name]  # Ugly, but works.
        frame = frame_class(parent=self.container, controller=self)
        frame.grid(row=0, column=0, sticky="nsew")
        frame.tkraise()
        self.current_frame = frame


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(
            self,
            text="This is the start page",
            font=controller.title_font,
        )
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(
            self,
            text="Define the number of networks",
            command=lambda: controller.show_frame("PageOne"),
        )
        button1.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(
            self,
            text="Define the number of networks",
            font=controller.title_font,
        )
        label.pack(side="top", fill="x", pady=10)

        nwnum = tk.Entry(self, textvariable=controller.num_networks)
        nwnum.pack()

        button1 = tk.Button(
            self,
            text="Go to the start page",
            command=lambda: controller.show_frame("StartPage"),
        )
        button1.pack()

        button2 = tk.Button(
            self,
            text="Define the networks",
            command=lambda: controller.show_frame("PageTwo"),
        )
        button2.pack()


class PageTwo(tk.Frame):
    def getent1(self):
        f = sys.stdout
        f.write("--- #" + "\n")
        f.write("networks:" + "\n")
        for entvar in self.entry_vars:
            value = entvar.get()
            if value:
                f.write(
                    "- { cloud: cloud1, network: "
                    + value
                    + " }"
                    + "\n"
                )

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(
            self,
            text="Define the networks",
            font=controller.title_font,
        )
        label.pack(side="top", fill="x", pady=10)
        self.entries = []
        self.entry_vars = []

        for t in range(1, self.controller.num_networks.get()):
            entvar = tk.StringVar()
            ent = tk.Entry(self, textvariable=entvar)
            self.entry_vars.append(entvar)
            self.entries.append(ent)
            ent.pack()
        button3 = tk.Button(
            self,
            text="Go to the start page",
            command=lambda: controller.show_frame("StartPage"),
        )
        button3.pack()
        button4 = tk.Button(
            self, text="Create the networks", command=self.getent1
        )
        button4.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()