pycharm创建模块和获取数据

时间:2018-04-18 09:15:20

标签: pycharm tkinter-entry

我正在尝试在pycharm中创建一个模块。第一个文件是“gui.py”,它包含为日期变量初始化的tkinter条目小部件。第二个文件“demoModule.py是我的模块文件。如何提取日期数据并使用from gui.py打印出来。我得到的错误是”NameError:name'date_box'未定义“

> gui.py

from tkinter import *
import datetime
import demoModule

root = Tk()
root.geometry('+0+0')
root.configure(bg='black')

frame_header = Frame(root, relief='groove')
frame_header.grid(row=0, column=0, sticky=W)

label = Label(frame_header, text='Date')
label.grid(row=0, column=0, sticky=N + S)

date_now = StringVar(frame_header, value=f'{datetime.datetime.now():%d-%m-%Y}')
date_box = Entry(frame_header, width=10, textvariable=date_now, justify='center')
date_box.grid(row=0, column=1, padx=10, sticky=N + S)

demoModule.get_date()

root.mainloop()

> demoModule.py

def get_date():
    date_get = date_box.get()
    print('The date is ', date_get)

1 个答案:

答案 0 :(得分:1)

好的,我这样得到了.....

  

gui.py

from tkinter import *
import datetime
import demoModule

root = Tk()
root.geometry('+0+0')
root.configure(bg='black')

frame_header = Frame(root, relief='groove')
frame_header.grid(row=0, column=0, sticky=W)

label = Label(frame_header, text='Date')
label.grid(row=0, column=0, sticky=N + S)

date_now = StringVar(frame_header, value=f'{datetime.datetime.now():%d-%m-%Y}')
date_box = Entry(frame_header, width=10, textvariable=date_now, justify='center')
date_box.grid(row=0, column=1, padx=10, sticky=N + S)

def try_get():
    date_get = date_box.get()
    demoModule.print_date(date_get)

try_get()

root.mainloop()
  

demoModule.py

def print_date(dmy):
    print('The date is ', dmy)