【问题标题】:get variable in tkinter frame class and send it to another python script在 tkinter 框架类中获取变量并将其发送到另一个 python 脚本
【发布时间】:2015-06-11 09:10:20
【问题描述】:

我需要将一个变量从一个 tkinter 脚本发送到另一个 python 脚本。 我的代码是这样的。

import os  
import sys
from Tkinter import *
import Tkinter as tk

class SeaofBTCapp(tk.Tk):

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

        ## class parent and parameter

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class PageDigital(tk.Frame):

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

        entry = Entry(self,  width=13, font=("Helvetica", 20))
        entry.place(x=50, y=300)

        def OK():
            os.system('pri.py')
        button21 = tk.Button(self, text="OK", font=("Helvetica", 15), width=8,command=OK)
        button21.place(x=300, y=300)

write1=entry1["text"]
import pri
app = SeaofBTCapp()
app.mainloop()

我需要将条目内的字符串发送到 pri.py 并打印它
pri.py 脚本是这样的

from __main__ import *
print write1

谢谢

【问题讨论】:

    标签: python class tkinter


    【解决方案1】:

    尝试让 OK 函数将 entry.get() 传递给 pri.py 脚本,这应该将 StringVar 的值传递给您的脚本。

    如果您需要在传递/打印之前关闭框架,请将条目转换为成员变量 (self.entry = Entry(..)) 并使用 app.entry.get() 在类/函数之外访问它。

    EDIT 06/27:新代码示例。在这里你可以看到我如何能够使用类结构来访问从第一个Entry 字段获得的变量,并将其显示在ToplevelLabel 中。我希望这能够有所帮助,因为我不能 100% 确定您要做什么。

    from tkinter import *
    
    class Window():
    
        def __init__(self, root):
    
            self.f = Frame(root)
            self.f.pack()
    
            self.e = Entry(self.f, width = 20)
            self.e.pack(side = LEFT)
    
            self.b = Button(self.f, width = 15, text = "OK", command = lambda: self.OK(root))
            self.b.pack(side = RIGHT)
    
        def OK(self, root):
    
            print ("Accessing entry inside class: %s" %self.e.get()) #Could pass to pri.py here
            self.word = self.e.get() 
            new = self.newWindow()
            root.wait_window(new)
            root.destroy()
    
        def newWindow(self):
    
            def done(newWindow):
    
                newWindow.destroy()
    
            newWindow = Toplevel()
            newWindow.tkraise()  # <- Personally never needed this, but it's in
    
            l = Label(newWindow, text = "Here is the variable in a top window: %s" %self.word)  #Using the variable in another window
            l.pack(side = LEFT)
    
            b = Button(newWindow, text = "Done", command = lambda: done(newWindow))
            b.pack(side = RIGHT)
    
            return newWindow
    
    root = Tk()
    w = Window(root)
    w.f.mainloop()
    print ("Accessing entry outside class: %s" %w.word)  #Or here
    

    【讨论】:

    • 如果我想提升新框架并带上前一个框架变量,我该怎么办?对不起我的愚蠢,但我不能同时做 get 方法和 raise 方法
    • @Daniel Budiono 查看我最新的代码编辑;我不是 100% 确定你想要做什么,但我希望它能够帮助你。基本上我添加了如何创建一个新的Toplevel 窗口并在其中显示我的变量
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2021-11-12
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多