pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    1. <small id='JypLE'></small><noframes id='JypLE'>

      1. <tfoot id='JypLE'></tfoot>
          <bdo id='JypLE'></bdo><ul id='JypLE'></ul>

        <i id='JypLE'><tr id='JypLE'><dt id='JypLE'><q id='JypLE'><span id='JypLE'><b id='JypLE'><form id='JypLE'><ins id='JypLE'></ins><ul id='JypLE'></ul><sub id='JypLE'></sub></form><legend id='JypLE'></legend><bdo id='JypLE'><pre id='JypLE'><center id='JypLE'></center></pre></bdo></b><th id='JypLE'></th></span></q></dt></tr></i><div class="17x5jdf" id='JypLE'><tfoot id='JypLE'></tfoot><dl id='JypLE'><fieldset id='JypLE'></fieldset></dl></div>
        <legend id='JypLE'><style id='JypLE'><dir id='JypLE'><q id='JypLE'></q></dir></style></legend>

        為什么 tkinter 模塊在通過命令行運行時會引發屬

        Why tkinter module raises attribute error when run via command line but not when run via IDLE?(為什么 tkinter 模塊在通過命令行運行時會引發屬性錯誤,但在通過 IDLE 運行時不會?) - IT屋-程序員軟件開發技術分

        <small id='RWMXp'></small><noframes id='RWMXp'>

          <bdo id='RWMXp'></bdo><ul id='RWMXp'></ul>
              <tbody id='RWMXp'></tbody>
            <legend id='RWMXp'><style id='RWMXp'><dir id='RWMXp'><q id='RWMXp'></q></dir></style></legend>
              <tfoot id='RWMXp'></tfoot>

                1. <i id='RWMXp'><tr id='RWMXp'><dt id='RWMXp'><q id='RWMXp'><span id='RWMXp'><b id='RWMXp'><form id='RWMXp'><ins id='RWMXp'></ins><ul id='RWMXp'></ul><sub id='RWMXp'></sub></form><legend id='RWMXp'></legend><bdo id='RWMXp'><pre id='RWMXp'><center id='RWMXp'></center></pre></bdo></b><th id='RWMXp'></th></span></q></dt></tr></i><div class="fvtfj9h" id='RWMXp'><tfoot id='RWMXp'></tfoot><dl id='RWMXp'><fieldset id='RWMXp'></fieldset></dl></div>
                  本文介紹了為什么 tkinter 模塊在通過命令行運行時會引發屬性錯誤,但在通過 IDLE 運行時不會?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  與通過 IDLE 的 run module f5 命令運行相比,通過命令行運行代碼會引發錯誤是否有原因?

                  Is there a reason why the code will raise an error when run via the command line compared to when run via IDLE's run module f5 command?

                  最近我一直在努力提高我的代碼的可讀性和健壯性.結果,我一直在嘗試刪除所有 from module import * 行.我曾經使用 from tkinter import * 并且我的這行代碼運行良好:

                  Recently I've been trying to improve the readability and robust-ness of my code. As a result I've been trying to remove all the from module import * lines. I used to use from tkinter import * and this line of my code worked perfectly fine:

                  self.path = filedialog.askdirectory()

                  但現在我已將 from tkinter import * 更改為 import tkinter as tk 并相應地更改了代碼:

                  But now I have changed from tkinter import * to import tkinter as tk and I have changed the code accordingly:

                  self.path = tk.filedialog.askdirectory()

                  名為 GUI.py 的文件使用以下命令導入此文件:from lib.filesearch import *(我提到的代碼行位于文件搜索文件中.)

                  A file called GUI.py imports this file with: from lib.filesearch import * (the line of code I mentioned resides within the filesearch file.)

                  我通過 IDLE 運行我的代碼,一切都很好.我的 GUI 仍然有效,并且行 self.path = tk.filedialog.askdirectory() 正常工作,但是,當我通過 windows 命令行運行代碼時,我收到錯誤:

                  I run my code via IDLE and everything is fine. My GUI still works and the line self.path = tk.filedialog.askdirectory() works like normal however, when I run the code through windows command line I get the error:

                  AttributeError: 'module' object has no attribute 'filedialog'
                  

                  以下是我的代碼中的相關位:

                  Here are the relevant bits from my code:

                  來自文件搜索.py

                  import tkinter as tk
                      def get_path(self):
                          """Store user chosen path to search"""
                          self.paths = tk.filedialog.askdirectory(initialdir = FileSearch.DEFAULT)
                          return self.paths
                  

                  來自 GUI.py

                  from lib.filesearch import *    
                      def Browse(self):
                          self.BrowseB['state']='disabled'
                          self.p=self.CrawlObj.get_path()
                          self.AddText('Searching from Path: ' + str(self.p))
                          self.BrowseB['state']='normal'
                  

                  與此不同 問題 我只安裝了一個版本的 python.即 Python34.

                  Unlike this question I only have one version of python installed. Namely, Python34.

                  推薦答案

                  首先我想說:如果你知道你會使用子模塊,總是顯式地導入它們.這個答案的結尾有一個更令人信服的案例,這很重要.

                  I want to start by saying: always explicitly import submodules if you know you will use them. The end of this answer has a more compelling case where this is important.

                  由于 tkinter 的結構,您必須顯式導入子模塊才能加載:

                  Because of the structure of tkinter you must explicitly import submodules for them to load:

                  import tkinter as tk
                  print(hasattr(tk,"filedialog")) # in a standard interpreter will print false
                  import tkinter.filedialog
                  print(hasattr(tk,"filedialog")) # should always print true after explicit import
                  

                  您不需要在 IDLE 中執行此操作的原因是,在您的代碼運行之前,IDLE 在后臺設置了一些東西并最終導入了一些 tkinter 庫.維護者之一 已評論這實際上是 IDLE 中的一個錯誤.

                  the reason you don't need to do this in IDLE is that before your code is run IDLE sets up some stuff in the background and ends up importing some of the tkinter libraries. One of the maintainers has commented that this is effectively a bug in IDLE.

                  在 python 3.6.5 中(可能更早,僅檢查此版本)此特定差異已得到修復,因此除了我在下面顯示的 2 個模塊外,其他所有模塊都不會出現此問題.em>

                  In python 3.6.5 (and possibly earlier, only checked this version) this specific discrepancy has been fixed so it no longer happens for all but 2 modules I show below.

                  在任何版本中,您都可以看到加載了如下代碼的子模塊列表:

                  in any version you can see a list of submodules that are loaded with some code like this:

                  Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
                  # standard interpreter 
                  >>> import sys
                  >>> len(sys.modules) #total number of modules automatically loaded
                  71
                  >>> sorted(name for name in sys.modules.keys() if ("." in name)) #submodules loaded
                  ['collections.abc', 'encodings.aliases', 'encodings.latin_1', 'encodings.utf_8', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'os.path']
                  >>> len(_) #number of submodules
                  10
                  

                  在 IDLE 中:

                  Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
                  # IDLE
                  >>> import sys
                  >>> len(sys.modules)
                  152
                  >>> sorted(name for name in sys.modules.keys() if ("." in name and "idlelib" not in name))
                  ['collections.abc', 'encodings.aliases', 'encodings.ascii', 'encodings.latin_1', 'encodings.utf_8', 'importlib._bootstrap', 'importlib._bootstrap_external', 'importlib.abc', 'importlib.machinery', 'importlib.util', 'os.path', 'tkinter.constants', 'urllib.parse']
                  >>> len(_) #number of submodules not directly related to idlelib.
                  13
                  

                  tkinter.constants 是在您剛剛 import tkinter 時加載的,所以在我測試的版本中,這個問題仍然存在于僅 urllib.parseencodings.ascii (和 idlelib 模塊,但通常生產代碼不使用它)

                  tkinter.constants is loaded when you just import tkinter so as of the version I tested, this issue still exists for only urllib.parse and encodings.ascii (and idlelib modules but generally production code doesn't use that)

                  這不一定是 IDLE 特定的問題,更糟糕的問題是子模塊是否由您使用的另一個庫加載.以如下代碼為例:

                  This isn't necessarily an IDLE specific issue though, a worse issue is if the submodule is loaded by another library you use. Take the following code as an example:

                  >>> import pandas
                  >>> import http
                  >>> http.client
                  <module 'http.client' from '.../http/client.py'>
                  

                  現在假設我們編寫了一些仍然使用 http.client 但沒有使用 pandas 的其他代碼:

                  now lets say we wrote some other code that still used http.client but didn't use pandas:

                  >>> import http
                  >>> http.client
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  AttributeError: module 'http' has no attribute 'client'
                  

                  這樣,當使用它的代碼加載 http.client 時,您可能會得到一個子模塊,該子模塊可以正常工作,可能是通過使用碰巧使用它但會失敗的庫.

                  This way you could end up with a submodule that works properly when the code that uses it loads http.client possibly by using a library that happens to use it but will otherwise fail.

                  這將我帶回到我的初始點 - 始終顯式導入子模塊.

                  This takes me back to my initial point - always explicitly import submodules.

                  這篇關于為什么 tkinter 模塊在通過命令行運行時會引發屬性錯誤,但在通過 IDLE 運行時不會?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復導入頂級名稱的情況下構造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發帶有已編譯動態共享庫的 Python 包)

                  <legend id='DlIDN'><style id='DlIDN'><dir id='DlIDN'><q id='DlIDN'></q></dir></style></legend>
                    • <bdo id='DlIDN'></bdo><ul id='DlIDN'></ul>
                    • <i id='DlIDN'><tr id='DlIDN'><dt id='DlIDN'><q id='DlIDN'><span id='DlIDN'><b id='DlIDN'><form id='DlIDN'><ins id='DlIDN'></ins><ul id='DlIDN'></ul><sub id='DlIDN'></sub></form><legend id='DlIDN'></legend><bdo id='DlIDN'><pre id='DlIDN'><center id='DlIDN'></center></pre></bdo></b><th id='DlIDN'></th></span></q></dt></tr></i><div class="p5z7jjh" id='DlIDN'><tfoot id='DlIDN'></tfoot><dl id='DlIDN'><fieldset id='DlIDN'></fieldset></dl></div>
                      • <small id='DlIDN'></small><noframes id='DlIDN'>

                        <tfoot id='DlIDN'></tfoot>

                            <tbody id='DlIDN'></tbody>

                            主站蜘蛛池模板: 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 彼得逊采泥器-定深式采泥器-电动土壤采样器-土壤样品风干机-常州索奥仪器制造有限公司 | 洗砂机械-球磨制砂机-洗沙制砂机械设备_青州冠诚重工机械有限公司 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 微型实验室真空泵-无油干式真空泵-微型涡旋耐腐蚀压缩机-思科涡旋科技(杭州)有限公司 | 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 代办建筑资质升级-建筑资质延期就找上海国信启航 | 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 陕西鹏展科技有限公司| 阳光1号桔柚_无核沃柑_柑橘新品种枝条苗木批发 - 苧金网 | 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 培训无忧网-教育培训咨询招生第三方平台| 中开泵,中开泵厂家,双吸中开泵-山东博二泵业有限公司 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 山东钢格板|栅格板生产厂家供应商-日照森亿钢格板有限公司 | 环氧树脂地坪漆_济宁市新天地漆业有限公司| 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 贵州自考_贵州自学考试网| 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 臻知网大型互动问答社区-你的问题将在这里得到解答!-无锡据风网络科技有限公司 | 光伏支架成型设备-光伏钢边框设备-光伏设备厂家 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 劳动法网-专业的劳动法和劳动争议仲裁服务网 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 伺服电机维修、驱动器维修「安川|三菱|松下」伺服维修公司-深圳华创益 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 南京雕塑制作厂家-不锈钢雕塑制作-玻璃钢雕塑制作-先登雕塑厂 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 搪瓷反应釜厂家,淄博搪瓷反应釜-淄博卓耀 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 东莞市天进机械有限公司-钉箱机-粘箱机-糊箱机-打钉机认准东莞天进机械-厂家直供更放心! |