简体   繁体   English

为什么我不能在另一个.py文件中使用其中一个.py文件中的方法?

[英]Why I can't use method from one of my .py files in another .py file?

This is my code for form with button: 这是我的带有按钮的表单代码:

import sys
from ClickerBot import test_ClikerBot

def func1():
    if sys.version_info < (3, 0):
        # Python 2
        import Tkinter as tk
    else:
        # Python 3
        import tkinter as tk
    root = tk.Tk()
    root.title(“Clicker”)

   def startClick():
        root.destroy()
        clicker = test_ClikerBot()
        clicker.test_clicker_bot()

   button1 = tk.Button(root, text=“CLICK CLICK CLICK”, command=startClick())
    button1.pack()
    tk.mainloop()

if __name__ == ‘__main__‘:
    func1()

When I start this file, I receive next error: 当我启动此文件时,我收到下一个错误:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7    /Users/ltst/Desktop/dev/DribbleBot/venv/Scripts/main.py
Traceback (most recent call last):
  File “/Users/ltst/Desktop/dev/DribbleBot/venv/Scripts/main.py”, line 23, in <module>
    func1()
  File “/Users/ltst/Desktop/dev/DribbleBot/venv/Scripts/main.py”, line 19, in func1
    button1 = tk.Button(root, text=“CLICK CLICK CLICK”, command=startClick())
  File “/Users/ltst/Desktop/dev/DribbleBot/venv/Scripts/main.py”, line 16, in startClick
    clicker = test_ClikerBot()
  File “/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py”, line 191, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class ‘ClickerBot.test_ClikerBot’>: runTest

But in my ClickerBot class exist this method: 但是在我的ClickerBot类中存在此方法:

class test_ClikerBot(unittest.TestCase):    
   def test_clicker_bot(self):

Where is problem? 问题在哪里? I import this code from catalog studio recorder, and start learn python a week ago, because many things for me like magic and google help not every time (Sorry for bad English) 我从Catalog Studio记录器导入此代码,并在一周前开始学习python,因为对我来说,魔术和Google等许多功能并非每次都提供帮助(对不起,英语不好)

Your code would be ok if your test_ClikerBot class was an ordinary class. 如果您的test_ClikerBot类是普通类,那么您的代码就可以了。 The point is that unittest is a very special framework, and TestCase classes are not meant to be used directly, but thru a TestRunner and a TestLoader that both do quite some magic under the hood. 关键是unittest是一个非常特殊的框架,并且TestCase类不是要直接使用的,而是通过TestRunnerTestLoader ,它们在TestLoaderTestLoader了很大的作用。 This is partly documented , but you'll probably have to read the source code of the existing runners and loaders to really find out how to make the whole thing work. 这部分记录在案 ,但是您可能必须阅读现有的运行程序和装载程序的源代码,才能真正了解如何使整个工作正常进行。 A good starting point might be the already existing GUI testrunner mentionned in the doc: 一个好的起点可能是文档中提到的已经存在的GUI testrunner:

The script Tools/unittestgui/unittestgui.py in the Python source distribution is a GUI tool for test discovery and execution Python源代码分发中的脚本Tools / unittestgui / unittestgui.py是用于测试发现和执行的GUI工具

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

相关问题 为什么我不能使用从另一个 py 文件导入的属性? - why I cannot use a attribute that import from another py file? 为什么我无法将.ui文件转换为.py? - Why can't I covert my .ui file to .py? 我可以从另一个.py 文件中的一个 function 打印一个变量吗 - Can I print a variable from one function in another .py file 为什么我找不到扩展名为 py 的文件? - Why can't I find files that have py extension? 从一个.py文件到另一个.py文件的参数替换 - parameters substitution from one .py file to another .py file 在 another.py 文件的循环中使用 one.py 文件中的变量 - Using variables from one .py file in a loop in another .py file Jupyter:为什么不能使用包含Shell命令的方法导入`.py`文件? - Jupyter: why can't I import a `.py` file with a method containing a shell command? 如何循环遍历当前目录中的所有 .py 文件并从每个文件导入一个变量? - How can I loop through all the .py files in my current directory & import a variable from each one? 我无法将我的 py 文件打包为 exe 文件 - I can't package my py file to an exe file 如何在不熟悉从中获取.py文件的情况下解析所使用方法的结果? - How can I parse the results of a method that I use without being familiar with .py file it was taken from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM