简体   繁体   English

Python tkinter操作,用于从文本框中传递值

[英]Python tkinter operation for pass the value from the Textbox

Actually I have done a script for .docx operation. 实际上,我已经为.docx操作编写了脚本。 That script would count the total .docx files in the folder and give some reports. 该脚本将计算文件夹中的.docx文件总数并提供一些报告。 Now I plan to use a GUI operation on that. 现在,我计划对此使用GUI操作。 In my script I Put that folder which is presented in the python directory so it's working but I what I want here is, User will enter the path in textbox like "C:/user/app/data/folder_name" and when pressing of the submit button It will show the report. 在我的脚本中,我将该文件夹放置在python目录中,因此它可以正常工作,但我要在这里显示的是,用户将在文本框中输入路径,例如“ C:/ user / app / data / folder_name”,然后按提交按钮将显示报告。 Here I attached the code 在这里我附上了代码

code: 码:

import os
import glob
from docx import Document

from tkinter import *
def print_input():

    mypath = text_entry.get()
    files=0
    for name in os.listdir(mypath):
        if name.endswith('.docx'):
          files=files+1
    print("Total No of Files:",files)
    table=0
    for name in os.listdir(mypath):
         for word in glob.glob('*.docx'):
          doc=Document(word)
          for t in doc.tables:
            for ro in t.rows:
              if ro.cells[0].text=="ID" :
                table=table+1
    print("Total Number of Tables: ", table)

root = Tk()
Label(root, text="Enter Path").grid(row=0)

text_entry = Entry(root)
text_entry.grid(row=1, column=0)
Button(root, text='Submit', command=print_input).grid(row=3, column=0, sticky=W, pady=4)
mainloop()

This is my new code. 这是我的新代码。 But I have only one file in that folder and it consist 5 tables. 但是我在该文件夹中只有一个文件,它包含5个表。 This code giving 2 files and 312 tables. 这段代码给出了2个文件和312个表。 what can i do? 我能做什么?

You can use the get() method of the Entry widget to get what the user has input.So something like this should work: 您可以使用Entry小部件的get()方法获取用户Entry内容,因此应如下所示:

from tkinter import *
def get_path():
    #Something like this
    #replace your default path with the user's path
    mypath = text_entry.get()
    files=0
    for name in glob.glob(mypath):
    files=files+1
    print("Total No of Files:",files)

root = Tk()
Label(root, text="Enter Path").grid(row=0)
#Create text entry and add it to the window:
text_entry = Entry(root)
text_entry.grid(row=1, column=0)
#This button will call the get_path function when it is clicked
Button(root, text='Submit', command=get_path).grid(row=3, column=0, sticky=W, pady=4)
mainloop()

Edit:You could also use the Text widget to display your results when the same submit button is called 编辑:当调用相同的提交按钮时,您也可以使用“ Text小部件显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM