将变量从python脚本导入到python脚本的问题

时间:2017-08-30 16:33:30

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

我尝试将变量从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)

Max.py:

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

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

  

NameError:名称'Number'未定义

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

感谢您提前提供任何帮助:)

1 个答案:

答案 0 :(得分:0)

你需要使用Leo.number,否则python认为你是指一个局部变量。你想要在狮子座之外使用的狮子座中的变量也需要被声明为全局变量,或者它们不会在狮子座范围之外被访问。

希望这有帮助

加雷