Python 板


LINE

先上code ------------------------------------------------------------------- import os import cv2 import tkinter as tk from tkinter import filedialog # global variable class gloabl_variable: isExit = False saver_folder = None image_folder = None pred = None img = None name = None G = gloabl_variable() class main_tk(tk.Frame): def __init__(self,master): self.master=master tk.Frame.__init__(self,master) master.title("Menu") master.geometry("240x180") self.pack() self.createwidget() def createwidget(self): self.load_btn=tk.Button(self, text="Select Image Folder", height = 2, width = 20,command=self.clickLoad) self.load_btn['font'] =['Times', '14', 'normal'] self.load_btn.pack() self.select_btn=tk.Button(self, text="Select Saver Folder", height = 2, width = 20,command=self.clickSelect) self.select_btn['font'] =['Times', '14', 'normal'] self.select_btn.pack() self.start_btn=tk.Button(self, text="Start", height = 2, width = 20,command=self.clickStart) self.start_btn['font'] =['Times', '14', 'normal'] self.start_btn.pack() def clickLoad(self): options = {} options['title'] = "tkFileDialog.askdirectory" fs = filedialog.askdirectory(**options) if fs: print (fs) G.image_folder = fs else: print ("Without selected!") def clickSelect(self): options = {} options['title'] = "tkFileDialog.askdirectory" fs = filedialog.askdirectory(**options) if fs: print (fs) G.saver_folder = fs else: print ("Without selected!") def clickStart(self): self.master.destroy() for name in os.listdir(G.image_folder)[0:500]: G.img = os.path.join(G.image_folder, name) G.name = name[:-4] G.pred = name.split('_')[3][:-4] img = cv2.imread(G.img) cv2.namedWindow('img', cv2.WINDOW_NORMAL) cv2.imshow('img', img) pred = tk.Tk() pred_tk(master=pred) pred.mainloop() class pred_tk(tk.Frame): def __init__(self,master): self.master=master tk.Frame.__init__(self,master) master.title("pred") master.geometry("240x100+30+30") master.resizable(0, 0) master.attributes("-topmost", 1) self.pack() self.createwidget() def createwidget(self): self.label = tk.Label(self, text=G.pred) self.label['font'] = ['Times', '20', 'bold'] self.label.pack() self.e1 = tk.Entry(self) self.e1['font'] = ['Times', '20', 'normal'] self.e1.pack() self.e1.focus() self.e1.bind('<Return>', self.get) def get(self, event): self.rename_close() def rename_close(self): if self.e1.get() != '' and self.e1.get() != G.pred: if not os.path.isdir( os.path.join(G.saver_folder, self.e1.get()) ): os.mkdir(os.path.join(G.saver_folder, self.e1.get())) img1 = cv2.imread(G.img) cv2.imwrite( os.path.join(G.saver_folder, self.e1.get(), G.name+'_'+self.e1.get()+'.jpg'), img1) cv2.destroyAllWindows() self.master.destroy() else: cv2.destroyAllWindows() self.master.destroy() def main(): win=tk.Tk() main_tk(master=win) win.mainloop() G.isExit = True print("All exit \n") if __name__ == '__main__': main() ---------------------------------------------------------------------- code说明: 目的是要帮图片更名 这个程式首先会建一个名字叫 Menu 的窗口(main_tk) 它可以让我选择我要读的图片所在资料夹和我更名後要存的资料夹 点击 start 後 Menu 窗口会被我关掉 然後同时开起 opencv 读进去的图片画面和pred这个窗口(pred_tk) 我要做的事情是判断图片实际讯息跟我print在窗口上的讯息有没有吻合 如果不对的话我就会在下面输入正确讯息後按 enter 存档并同时关闭图片和窗口 然後再读入下一章图片并开起下一个窗口 ----------------------------------------------------------------------- 问题说明: 因为想要增加效率,所以希望每次打开的 pred_tk 窗口总是在最上层 (这样如果讯息没问题就直接 enter 换下一张就好) 所以我加上了 master.attributes("-topmost", 1) 但不知道为什麽他仍然会在整个windows视窗的最底层 请问要如何解决这个问题?谢谢大家! --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.132.83.205
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1511434887.A.A2F.html
1F:→ chliu6: 我觉得问题可能出现在你用cv2.imshow()所产生的视窗实际上 11/23 21:24
2F:→ chliu6: 与Tk没有任何关系 才会导致topmost无效 11/23 21:25
3F:→ kkagt: 好的,那我试试看不要用opencv开图片@@ 11/24 09:19
4F:→ kkagt: 用plt也没用QQ 11/24 09:25
5F:→ chliu6: 你可以考虑用Toplevel产生新的视窗 图片再放入新的视窗中 11/24 17:41
6F:→ chliu6: 再用一次Toplevel 做出一个对话视窗可以把对话视窗移到最 11/24 17:42
7F:→ chliu6: 上层 11/24 17:43
8F:→ chliu6: 不过用此方式你不能destroy TK()这个window 11/24 17:44
9F:→ kkagt: 好的,我研究看看..感谢Orz 11/27 15:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP