如何在多个框架中打印出ttk.Combobox选项?

时间:2019-12-05 18:24:10

标签: python tkinter

在我的代码中,有两个框架。在第一个中,我放入了一个 Add 按钮,它将产生一个带有Combobox的新帧。这个想法是在第一帧中添加一些Combobox,为不同的Combobox选择不同的选项,然后在下一帧中将它们打印出来。但是,当我在第二帧中按下Show options按钮时,它不会打印出我刚在第一帧中选择的选项。我该怎么解决?

from tkinter import *
from tkinter import ttk

list_1 = []


class Validation_Tool(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("PageOne")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def quit(self):
        self.destroy()


class PageOne(Frame):

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

        def add_compare():
            global list_1

            frame = Frame(self)
            frame.pack()

            label_1 = Label(frame, text='Options')
            label_1.grid(row=0, column=0)

            self.options_1 = ttk.Combobox(frame, values=['a','b','c','d','e'])
            self.options_1.grid(row=1, column=0)

            list_1.append(self.options_1.get())

        quit_button = Button(self, text="Quit Program",
                    command=lambda: controller.quit())
        next_button = Button(self, text="Next",
                    command=lambda: controller.show_frame("PageTwo"))
        add_button = Button(self, text='Add', command=add_compare)

        quit_button.place(relx=0.98, rely=0.98, anchor=SE)
        next_button.place(relx=0.76, rely=0.98, anchor=SE)
        add_button.place(relx=0.661, rely=0.98, anchor=SE)


class PageTwo(Frame):

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

        def button():
            label = Label(self, text=list_1)
            label.pack()

        quit_button = Button(self, text="Quit Program",
                            command=lambda: controller.quit())
        back_button = Button(self, text="Back",
                            command=lambda: controller.show_frame("PageOne"))
        show_button = Button(self, text='Show options', command=button)
        show_button.pack()

        back_button.place(relx=0.76, rely=0.98, anchor=SE)
        quit_button.place(relx=0.98, rely=0.98, anchor=SE)


if __name__ == "__main__":
    root = Validation_Tool()
    root.geometry('400x300+430+250')
    root.title("Validation Tool")
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

这是您代码的修改版本,当按 Next 时,将打印到目前为止选择的选项。为了防止library(shiny) library(leaflet) library(glue) ui <- fluidPage( leafletOutput('map'), textOutput('tile') ) server <- function(input, output, session) { output$map <- renderLeaflet({ leaflet() %>% addTiles(options = tileOptions(noWrap = TRUE)) }) tile_finder <- eventReactive(input$map_click, { long <- input$map_click$lng lat <- input$map_click$lat zoom <- input$map_zoom lambda <- pi/180*long phi <- pi/180*lat x <- (2^zoom*(lambda + pi)/(2*pi))%/%1 y <- (2^zoom*(pi - log(tan(phi) + 1/(cos(phi))))/(2*pi))%/%1 glue("https://tile.openstreetmap.org/{zoom}/{x}/{y}.png") }) output$tile <- renderText({ tile_finder() }) } shinyApp(ui, server) 相互干扰,保留了它们和相关的Combobox的列表。

使用单独的StringVar可以避免在其中一个选项上选择一个选项而在另一个选项上进行更改的问题,即每个选项都与一个不同的StringVar相关联。

为了将所有选项收集到textvar中,已定义了名为list_1的回调函数,并将其“绑定”到selected()选择事件中。这样,除上述内容外,所选选项还将附加到全局Combobox上,这是 Show options 按钮显示的内容。

list_1