如何通过我的switch frame函数将Tkinter条目值从一个框架传到另一个框架?[英] How to pass Tkinter entry value from one frame to another through my switch frame function

本文是小编为大家收集整理的关于如何通过我的switch frame函数将Tkinter条目值从一个框架传到另一个框架?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的TKINTER应用程序已添加了笔记本电脑,在笔记本电脑中,我想使用按钮切换框架.实现的笔记本开关和帧开关.当我单击"好的"按钮enter code here

我试图将值传递为帧类初始化的参数

输入的值分配给全局变量

在帧中:类Tab1_frame1 想要从self.uidentry = entry(self,bd = 5)传递值

import tkinter as tk
from tkinter import *
from tkinter import ttk

# Root class to create the interface and define the controller function to switch frames
class RootApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(NoteBook)

# controller function
    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

# sub-root to contain the Notebook frame and a controller function to switch the tabs within the notebook
class NoteBook(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.notebook = ttk.Notebook()
        self.tab1 = Tab1(self.notebook)
        self.notebook.add(self.tab1, text="User Bash History")
        self.notebook.pack()

# controller function
    def switch_tab1(self, frame_class):
        new_frame = frame_class(self.notebook)
        self.tab1.destroy()
        self.tab1 = new_frame

# Notebook - Tab 1
class Tab1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self._frame = None
        self.switch_frame(Tab1_Frame1)

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


# first frame for Tab1
class Tab1_Frame1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="Userbash history")

        self.userid = Label(self, text ="User ID", bg="blue", fg="white")
        self.userid.pack()
        self.newWindow = None
        self.uidentry = Entry(self, bd=5)
        self.uidentry.pack()

        global uid
        uid = self.uidentry.get()
        # button object with command to replace the frame
        self.button = Button(self, text="OK", command=lambda: master.switch_frame(Tab1_Frame2))
        self.label.pack()
        self.button.pack()

    def new_window(self):
        if self.newWindow is not None:
            self.newWindow.destroy()
        self.newWindow = Frame(self)

        self.uid=self.uidentry.get()
        self.app = logwindow(self.newWindow, self.uid)     

# second frame for Tab1
class Tab1_Frame2(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="it has been changed!")       

        # and another button to change it back to the previous frame
        self.button = Button(self, text="self" , command=lambda: master.switch_frame(Tab1_Frame1))
        self.label.pack()
        self.button.pack()
    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.uid=self.uidentry.get()




if __name__ == "__main__":
    Root = RootApp()
    Root.geometry("640x480")
    Root.title("My Host APP")
    Root.mainloop()```

推荐答案

您的按钮使用Master self.master.switch_frame()的方法,仅切换选项卡

self.button = Button(self, text="OK", command=lambda: master.switch_frame(Tab1_Frame2))

但是它可以使用自己的方法,该方法将使用self.master.switch_frame()切换选项卡,并且还将将值直接移动到新选项卡:

    self.button = tk.Button(self, text="OK", command=self.switch_frame)

def switch_frame(self):
    # get value from Entry in current Tab
    value = self.uidentry.get()

    # switch tab
    self.master.switch_frame(Tab1_Frame2)

    # use value directly in new Tab
    self.master._frame.uidentry = value
    self.master._frame.label['text'] = value

或to master和其他选项卡可以在__init__中获取它:

    self.button = tk.Button(self, text="OK", command=self.switch_frame)

def switch_frame(self):
    # keep uidentry in master
    self.master.uidentry = self.uidentry.get()

    # switch tab
    self.master.switch_frame(Tab1_Frame2)

class Tab1_Frame2(tk.Frame):

    def __init__(self, *args, **kwargs):

        # get uidentry from master
        self.uidentry = self.master.uidentry
        self.label['text'] = self.master.uidentry

每个选项卡在switch_frame

中可能具有不同的代码

一开始就以默认值创建此变量是很好的.

在这里

完整代码:

import tkinter as tk
from tkinter import ttk


class RootApp(tk.Tk):

    def __init__(self):
        super().__init__()

        self.geometry("640x480")
        self.title("My Host APP")

        self._frame = None
        self.switch_frame(NoteBook)

    def switch_frame(self, frame_class):

        new_frame = frame_class(self)

        if self._frame:
            self._frame.destroy()

        self._frame = new_frame
        self._frame.pack()


class NoteBook(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.notebook = ttk.Notebook()
        self.tab1 = Tab1(self.notebook)
        self.notebook.add(self.tab1, text="User Bash History")
        self.notebook.pack()

    def switch_tab1(self, frame_class):
        new_frame = frame_class(self.notebook)
        self.tab1.destroy()
        self.tab1 = new_frame


class Tab1(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.uid = '' # default value at start

        self._frame = None
        self.switch_frame(Tab1_Frame1)

    def switch_frame(self, frame_class):

        new_frame = frame_class(self)

        if self._frame:
            self._frame.destroy()

        self._frame = new_frame
        self._frame.pack()


# first frame for Tab1
class Tab1_Frame1(tk.Frame):

    def __init__(self, *args, **kwargs):
        global uid

        super().__init__(*args, **kwargs)

        self.label = tk.Label(self, text="Userbash history")
        self.label.pack()

        self.userid = tk.Label(self, text ="User ID", bg="blue", fg="white")
        self.userid.pack()

        self.newWindow = None

        self.uidentry = tk.Entry(self, bd=5)
        self.uidentry.pack()

        uid = self.uidentry.get()

        # button object with command to replace the frame
        self.button = tk.Button(self, text="OK", command=self.switch_frame)
        self.button.pack()

    def switch_frame(self):
        # get value from Entry in current Tab
        value = self.uidentry.get()

        # keep value in master
        self.master.uid = value

        # switch tab
        self.master.switch_frame(Tab1_Frame2)

        # use value directly in new Tab
        #self.master._frame.uid = value
        #self.master._frame.label['text'] = value

    def new_window(self):

        if self.newWindow:
            self.newWindow.destroy()

        self.newWindow = tk.Frame(self)

        self.uid = self.uidentry.get()
        self.app = logwindow(self.newWindow, self.uid)     


class Tab1_Frame2(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.label = tk.Label(self, text="it has been changed!")       
        self.label.pack()

        # get value from master
        self.uid = self.master.uid
        self.label['text'] = self.uid

        # and another button to change it back to the previous frame
        self.button = tk.Button(self, text="self", command=lambda: self.master.switch_frame(Tab1_Frame1))
        self.button.pack()

    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)

        self.uid = self.uidentry.get()


if __name__ == "__main__":
    root = RootApp()
    root.mainloop()

本文地址:https://www.itbaoku.cn/post/2091138.html

问题描述

My Tkinter application have added Notebook and inside the notebook I want to switch the frame using a button. Implemented notebook switch and frame switch. i want to take entry input from one frame of notebook to another frame when I click 'okay' buttonenter code here

I tried to pass the value as argument for frame class initialization

assign the entry filed value to a global variable

In Frame : class Tab1_Frame1 want to pass value from self.uidentry = Entry(self, bd=5) to class Tab1_Frame2

import tkinter as tk
from tkinter import *
from tkinter import ttk

# Root class to create the interface and define the controller function to switch frames
class RootApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(NoteBook)

# controller function
    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()

# sub-root to contain the Notebook frame and a controller function to switch the tabs within the notebook
class NoteBook(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.notebook = ttk.Notebook()
        self.tab1 = Tab1(self.notebook)
        self.notebook.add(self.tab1, text="User Bash History")
        self.notebook.pack()

# controller function
    def switch_tab1(self, frame_class):
        new_frame = frame_class(self.notebook)
        self.tab1.destroy()
        self.tab1 = new_frame

# Notebook - Tab 1
class Tab1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self._frame = None
        self.switch_frame(Tab1_Frame1)

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


# first frame for Tab1
class Tab1_Frame1(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="Userbash history")

        self.userid = Label(self, text ="User ID", bg="blue", fg="white")
        self.userid.pack()
        self.newWindow = None
        self.uidentry = Entry(self, bd=5)
        self.uidentry.pack()

        global uid
        uid = self.uidentry.get()
        # button object with command to replace the frame
        self.button = Button(self, text="OK", command=lambda: master.switch_frame(Tab1_Frame2))
        self.label.pack()
        self.button.pack()

    def new_window(self):
        if self.newWindow is not None:
            self.newWindow.destroy()
        self.newWindow = Frame(self)

        self.uid=self.uidentry.get()
        self.app = logwindow(self.newWindow, self.uid)     

# second frame for Tab1
class Tab1_Frame2(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, text="it has been changed!")       

        # and another button to change it back to the previous frame
        self.button = Button(self, text="self" , command=lambda: master.switch_frame(Tab1_Frame1))
        self.label.pack()
        self.button.pack()
    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.uid=self.uidentry.get()




if __name__ == "__main__":
    Root = RootApp()
    Root.geometry("640x480")
    Root.title("My Host APP")
    Root.mainloop()```

推荐答案

Your button uses method from master self.master.switch_frame() which only switch tabs

self.button = Button(self, text="OK", command=lambda: master.switch_frame(Tab1_Frame2))

but it could use own method which will use self.master.switch_frame() to switch tabs and it will also move value(s) from current tab directly to new tab:

    self.button = tk.Button(self, text="OK", command=self.switch_frame)

def switch_frame(self):
    # get value from Entry in current Tab
    value = self.uidentry.get()

    # switch tab
    self.master.switch_frame(Tab1_Frame2)

    # use value directly in new Tab
    self.master._frame.uidentry = value
    self.master._frame.label['text'] = value

or to master and other tabs can get it in __init__ :

    self.button = tk.Button(self, text="OK", command=self.switch_frame)

def switch_frame(self):
    # keep uidentry in master
    self.master.uidentry = self.uidentry.get()

    # switch tab
    self.master.switch_frame(Tab1_Frame2)

class Tab1_Frame2(tk.Frame):

    def __init__(self, *args, **kwargs):

        # get uidentry from master
        self.uidentry = self.master.uidentry
        self.label['text'] = self.master.uidentry

Every Tab may have different code in switch_frame

It is good to create this variable in master at start with default value.

Here master is Tab1 but using master.master you can keep it in NoteBook, and using master.master.master in RootApp


Full code:

import tkinter as tk
from tkinter import ttk


class RootApp(tk.Tk):

    def __init__(self):
        super().__init__()

        self.geometry("640x480")
        self.title("My Host APP")

        self._frame = None
        self.switch_frame(NoteBook)

    def switch_frame(self, frame_class):

        new_frame = frame_class(self)

        if self._frame:
            self._frame.destroy()

        self._frame = new_frame
        self._frame.pack()


class NoteBook(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.notebook = ttk.Notebook()
        self.tab1 = Tab1(self.notebook)
        self.notebook.add(self.tab1, text="User Bash History")
        self.notebook.pack()

    def switch_tab1(self, frame_class):
        new_frame = frame_class(self.notebook)
        self.tab1.destroy()
        self.tab1 = new_frame


class Tab1(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.uid = '' # default value at start

        self._frame = None
        self.switch_frame(Tab1_Frame1)

    def switch_frame(self, frame_class):

        new_frame = frame_class(self)

        if self._frame:
            self._frame.destroy()

        self._frame = new_frame
        self._frame.pack()


# first frame for Tab1
class Tab1_Frame1(tk.Frame):

    def __init__(self, *args, **kwargs):
        global uid

        super().__init__(*args, **kwargs)

        self.label = tk.Label(self, text="Userbash history")
        self.label.pack()

        self.userid = tk.Label(self, text ="User ID", bg="blue", fg="white")
        self.userid.pack()

        self.newWindow = None

        self.uidentry = tk.Entry(self, bd=5)
        self.uidentry.pack()

        uid = self.uidentry.get()

        # button object with command to replace the frame
        self.button = tk.Button(self, text="OK", command=self.switch_frame)
        self.button.pack()

    def switch_frame(self):
        # get value from Entry in current Tab
        value = self.uidentry.get()

        # keep value in master
        self.master.uid = value

        # switch tab
        self.master.switch_frame(Tab1_Frame2)

        # use value directly in new Tab
        #self.master._frame.uid = value
        #self.master._frame.label['text'] = value

    def new_window(self):

        if self.newWindow:
            self.newWindow.destroy()

        self.newWindow = tk.Frame(self)

        self.uid = self.uidentry.get()
        self.app = logwindow(self.newWindow, self.uid)     


class Tab1_Frame2(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.label = tk.Label(self, text="it has been changed!")       
        self.label.pack()

        # get value from master
        self.uid = self.master.uid
        self.label['text'] = self.uid

        # and another button to change it back to the previous frame
        self.button = tk.Button(self, text="self", command=lambda: self.master.switch_frame(Tab1_Frame1))
        self.button.pack()

    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)

        self.uid = self.uidentry.get()


if __name__ == "__main__":
    root = RootApp()
    root.mainloop()