2017-08-30 68 views
-4

我想從我的Leo.py文件導入變量我Max.py文件是這樣的:Leo.py:與進口從python腳本變量python腳本問題

> 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: name 'Number' is not defined

如何使用Max.pyLeo.py的變量?

感謝提前任何幫助:)

+2

請嘗試格式化你的代碼。 –

+0

請原諒我亂碼。我對此很新。 :) – Leo

回答

0

您需要使用Leo.number否則蟒蛇認爲你指的是局部變量。你想在獅子座外面使用的獅子座變量也需要聲明爲全球獅子座,否則他們將不能在獅子座範圍之外訪問。

希望這有助於

加雷思

+0

如果我沒有錯,變量Number被聲明爲全局變量,但是在Max.py文件中使用Leo.Number而不是Number來運行腳本時,我得到消息「module'Leo'沒有屬性'Number 「」 – Leo