鱼C论坛

 找回密码
 立即注册
查看: 1450|回复: 2

求问一个关于tkinter,调用函数的问题

[复制链接]
发表于 2019-8-16 22:54:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我做了一个打开并查看图片的tkinter的界面如下
  1. from tkinter import *
  2. import tkinter as tk
  3. from tkinter import filedialog
  4. from PIL import Image, ImageTk
  5. import cv2
  6. import numpy as np
  7. import os

  8. if __name__ == "__main__":
  9.     root = Tk()
  10.     root.minsize(700,500)

  11.     #setting up a tkinter canvas with scrollbars
  12.     frame = Frame(root, bd=2, relief=SUNKEN)
  13.     frame.grid_rowconfigure(0, weight=1)
  14.     frame.grid_columnconfigure(0, weight=1)
  15.     xscroll = Scrollbar(frame, orient=HORIZONTAL)
  16.     xscroll.grid(row=1, column=0, sticky=E+W)
  17.     yscroll = Scrollbar(frame)
  18.     yscroll.grid(row=0, column=1, sticky=N+S)
  19.     canvas = Canvas(frame, bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
  20.     canvas.grid(row=0, column=0, sticky=N+S+E+W)
  21.     xscroll.config(command=canvas.xview)
  22.     yscroll.config(command=canvas.yview)
  23.     frame.pack(fill=BOTH,expand=1)


  24.     #function to be called when mouse is clicked
  25.     def printcoords():
  26.         file = filedialog.askopenfilename(parent=root, initialdir="D:/", title='Choose an image.')
  27.         filename = ImageTk.PhotoImage(Image.open(file))
  28.         canvas.image = filename  # <--- keep reference of your image
  29.         canvas.create_image(0, 0, anchor='nw', image=filename)
  30.         if file != '':
  31.             lb.config(text = "您选择的文件是:"+file);
  32.         return file
  33.         print(file)

  34.     lb = Label(root, text='')
  35.     lb.pack()
  36.     def co():
  37.         str = ('python picture_cut.py')
  38.         p = os.system(str)


  39.     Button(root,text='选择图片',command=printcoords).pack()
  40.     btn1=Button(root,text='计算',command=co)
  41.     btn1.place(x=100, y=450)

  42.     Button(root, text='清除').place(x=600, y=450)
  43.     root.mainloop()
复制代码

我想在另外一个文件中导入 printcoords():函数中file这个文件路径应该如何导入下面这个文件的img = cv2.imread(file)中

  1. import cv2
  2. import numpy as np
  3. import os
  4. from asd import printcoords



  5. root = 'd:\\TensorFlow\\picture\\'
  6. dsize = 56 #归一化处理的图像大小
  7. [color=Red]img = cv2.imread([size=5]file[/size])[/color]
  8. data = np.array(img)
  9. len_x = data.shape[0]
  10. len_y = data.shape[1]
  11. min_val = 30#设置最小的文字像素高度,防止切分噪音字符


  12. start_i = -1
  13. end_i = -1
  14. rowPairs = [] #存放每行的起止坐标

  15. #行分割
  16. for i in range(len_x):
  17.     if(not data[i].all() and start_i < 0):
  18.         start_i = i
  19.     elif(not data[i].all()):
  20.         end_i = i
  21.     elif (data[i].all() and start_i >= 0):
  22.         #print(end_i - start_i)
  23.         if(end_i - start_i >= min_val):
  24.             rowPairs.append((start_i, end_i))
  25.         start_i, end_i = -1, -1

  26. #print(rowPairs)

  27. #列分割
  28. start_j = -1
  29. end_j = -1
  30. min_val_word = 20  #最小文字像素长度
  31. number = 0 #分割后保存编号
  32. for start, end in rowPairs:
  33.     for j in range(len_y):
  34.         if(not data[start: end, j].all() and start_j < 0):
  35.             start_j = j
  36.         elif(not data[start: end, j].all()):
  37.              end_j = j
  38.         elif(data[start: end, j].all() and start_j >= 0):
  39.             if(end_j - start_j >= min_val_word):
  40.                 #print(end_j - start_j)
  41.                 tmp = data[start: end, start_j: end_j]
  42.                 im2save = cv2.resize(tmp, (dsize,dsize)) #归一化处理
  43.                 cv2.imwrite(root + '%d.jpg' % number, im2save)
  44.                 number += 1
  45.                 #print("%d  pic" % number)
  46.             start_j, end_j = -1, -1
复制代码

感谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-17 08:19:51 | 显示全部楼层
函数定义的部分放到 if __name__ == "__main__":前
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-17 12:52:05 | 显示全部楼层
你是不是,想在第二个py文件里面,引用第一个py文件的某函数的返回值呢?
可以直接在第二个py文件写 from 第一个py文件名 import 某函数/对象
比如,我在同一个工作目录下,建两个文件,文件名分别是demo.py和demo2.py
demo.py里写上
  1. def x():
  2.     b=5*3
  3.     return(b)
复制代码


在demo2.py里写
  1. from demo import x

  2. print(x())
复制代码


--------
你第一个py文件file返回了,那在第二个py文件里加上 from 第一个py文件名 import printcoords
你的程序貌似已经导入了...from asd import printcoords
那么这样调用就行
  1. file_path=printcoords()
  2. [color=Red]img = cv2.imread([size=5]file_path[/size])[/color]
复制代码


tkinter和cv2还没用过,不知道我理解得对不对...


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-17 17:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表