将用户输入从TKinter按钮导入到另一个.py模块

时间:2018-01-22 14:57:51

标签: python button tkinter python-import

const users = [{ teacher: [{ file: 'chemistry', size: '2MB' }, { file: 'math', size: '1MB' } ] }, { student: [{ file: 'chemistry', size: '3MB' }, { file: 'math', size: '4MB' } ] }]; let final = {}; users.forEach(function(i) { for (key in i) { var obj = {}; i[key].forEach(function(j) { let filesizestring = 'newfilesize' + j.size; obj[j.file] = filesizestring; }); final[key] = obj; } }); console.log(final);模块上,我有一个Tkinter帧; ticket.py,在同一个模块中,我有一个按钮value = Entry(frame);

command=exoutput

当我点击按钮时,我想在按钮命令/上导入def exoutput(): print value.get()value。 目前,当我导入时,othermodule.py是从print函数生成的,而不是从exoutput()文件生成的。

关于如何othermodule.py print value的建议?

othermodule.py

另一个文件,我尝试过这样的事情;

# ticket.py
from Tkinter import*

window = Tk()
window.title("Entry")

frame = Frame(window)
value = Entry(frame)

def exoutput():
    print value.get()

btnStage = Button(frame, text='ACTION', command=exoutput)
btnStage.pack(side=RIGHT, padx=2)
value.pack(side=LEFT)
frame.pack(padx=10, pady=10)

window.resizable(0, 0)
window.mainloop()

1 个答案:

答案 0 :(得分:0)

我认为你要么需要多线程,要么交换你的文件内容。在mainloop之后没有任何内容运行,直到Tk实例被销毁。

或者,您可以在OOP中构建ticket.py,并通过othermodule从中获取GUI对象,以便随意使用它。以下是一个例子:

ticket.py

#import tkinter as tk
import Tkinter as tk

class Window(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Entry")
        self.resizable(False, False)

        # create widgets
        self.frame = tk.Frame(self)
        self.value = tk.Entry(self.frame)
        self.btn_stage = tk.Button(self.frame, text="ACTION")

        # widgets layout
        self.frame.pack(padx=10, pady=10)
        self.btn_stage.pack(side='right', padx=2)
        self.value.pack(side='left')

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

othermodule.py

import ticket


def put():
    global my_var_in_othermodule

    my_var_in_othermodule = ticket_GUI.value.get()
    ticket_GUI.destroy()


my_var_in_othermodule = ""

ticket_GUI = ticket.Window()
ticket_GUI.btn_stage['command'] = put
ticket_GUI.mainloop()

print(my_var_in_othermodule)

input()
相关问题