【问题标题】:Problems with importing variables from python script to python script将变量从 python 脚本导入 python 脚本的问题
【发布时间】:2018-02-08 11:22:07
【问题描述】:

我尝试将变量从我的Leo.py 文件导入到我的Max.py 文件中,如下所示:Leo.py:

> import tkinter import time import os import Max from tkinter import *
> from tkinter import messagebox as tkMessageBox from selenium import
> webdriver from selenium.webdriver.common.by import By from
> selenium.webdriver.support.ui import WebDriverWait from
> selenium.webdriver.support import expected_conditions as EC from
> selenium.webdriver.common.keys import Keys from
> selenium.webdriver.common.action_chains import ActionChains from time
> import sleep from selenium.webdriver.support.ui import WebDriverWait
> 
> 
>     def combined_func(*args, **kwargs):
>         for f in funcs:
>             f(*args, **kwargs)
>     return combined_func
> 
> def Name():
>     global Name
>     Name = t.get()
> 
> def Message():
>     global Message
>     Message = t2.get()
> 
> def Number():
>     global Number
>     Number_string = t3.get()
>     Number = int(Number_string)
> 
> def Delay():
>     global Delay
>     Delay_string= t4.get()
>     Delay = int(Delay_string)
> 
> def close_window ():
>     root.destroy()
> 
> root = Tk()
> 
> w = Label(width = '40', height = '0') w.pack()
> 
> t = Entry(root) t.insert(INSERT, 'Insert Name') t.pack()
> 
> t2 = Entry(root) t2.insert(INSERT, 'Insert Messsage') t2.pack()
> 
> t3 = Entry(root) t3.insert(INSERT, 'Insert Number') t3.pack()
> 
> t4 = Entry(root) t4.insert(INSERT, 'Insert Delay') t4.pack()
> 
> b = Button(root, text='Run', command = combine_funcs(Name, Message,
> Number, Delay, close_window)) b.pack()
> 
> mainloop()
> 
> os.system('Max.py')
> 
> browser = webdriver.Chrome() browser.get('https://web.whatsapp.com/')
> wait = browser.implicitly_wait(3)
> 
> time.sleep(10)
> 
> N=1 Spam = 1
> 
> actions = ActionChains(browser) for _ in range(N):
>     actions = actions.send_keys(Keys.TAB) actions.perform()
> 
> for _ in range(N):
>     actions = actions.send_keys(Name) actions.perform()
> 
> time.sleep(2)
> 
> for _ in range(N):
>     actions = actions.send_keys(Keys.TAB) actions.perform()
> 
> time.sleep(2)
> 
> for _ in range(N):
>     actions = actions.send_keys(Keys.ENTER) actions.perform()
> 
> time.sleep(2)
> 
> while (Spam <= Number):
> 
>     for _ in range (1):
>         actions = actions.send_keys(Message, Keys.ENTER)
>     actions.perform()
>     Spam += 1
>     actions.reset_actions()
>     time.sleep(Delay)

最大.py:

> import Leo from Leo import * import tkinter from tkinter import *
> 
> root = Tk()
> 
> count = Label(root, text = Number)
> 
> mainloop()

但是运行Leo.py 脚本总是给我错误

NameError: name 'Number' is not defined

如何在Max.py 中使用来自Leo.py 的变量?

提前感谢您的帮助:)

【问题讨论】:

  • 请尝试格式化您的代码。
  • 请原谅我的乱码;我对此很陌生。 :)

标签: python python-3.x python-import


【解决方案1】:

你需要使用 Leo.number 否则 python 认为你指的是一个局部变量。您想在 Leo 之外使用的 Leo 中的变量也需要声明为全局变量,否则它们将无法在 Leo 范围之外访问。

希望对你有帮助

加雷斯

【讨论】:

  • 如果我没记错的话,变量 Number 被声明为全局变量,但是在使用 Leo.Number 而不是 Max.py 文件中的 Number 运行脚本时,我收到消息“module 'Leo'没有属性“数字””
猜你喜欢
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多