我在导入包含 tkinter 的模块时遇到问题。我不想粘贴代码,因为它超长,这不是我的问题,我使用 tkinter 和 selenium 创建了一个模块,这个模块打开一个网络浏览器并从验证码中获取快照,将其存储在我的本地桌面,然后将其显示在 tkinter 窗口中,当我在模块中执行此功能时,它可以正常工作,没有问题,当我将此模块导入要使用此功能的主模块时,问题就开始了,当我执行它时,网络浏览器完美启动,拍摄快照,然后当窗口应该弹出时......什么都没有发生,代码不会停止工作,会引发任何错误......我一直在寻找这个问题的答案行为不端,但没有找到类似的东西
from selenium import webdriver
import tkinter as tk
from PIL import Image, ImageTk
# from tkinter import filedialog
from pathlib import Path
# webDriv_path = Path(__file__).parent.__str__()
def getCaptchaImg(driver, path):
captchaImg = driver.find_element_by_css_selector("#ValidateCodeServlet")
captchaImg.screenshot(path + "\captcha_img.png")
def captchaInterface(path):
def closeWindow():
top.destroy()
def getCaptcha():
captchaCode.set(captchaBox.get())
closeWindow()
top = tk.Tk()
top.title("Captcha code")
captchaCode = tk.StringVar()
image = Image.open(path + "\captcha_img.png")
my_img = ImageTk.PhotoImage(image)
my_img_lab = tk.Label(image=my_img)
captchaBox = tk.Entry(width=13) # captcha box creation
submitButt = tk.Button(top, text="Go!", width=10, height=3, command=getCaptcha) # submit butt creation
my_img_lab.place(x=5, y=0) # Captcha image pack
captchaBox.place(x=6, y=37) # Captcha box pack
submitButt.place(x=95, y=2) # submit butt pack
top.geometry("180x70+750+450")
top.attributes("-toolwindow", True)
top.resizable(width=False, height=False)
top.mainloop()
return captchaCode.get()
def submitFileInterface():
def closeWindow():
top.destroy()
def getFilePath():
cspFilePath.set(tk.filedialog.askopenfile(filetypes=[("Excel file", ".csv")]).name)
cspFile.set(cspFilePath.get().split('/')[-1])
top = tk.Tk()
top.title("Submit file")
cspFile = tk.StringVar(top)
cspFilePath = tk.StringVar(top)
fileSel = tk.Label(top, text="Selected File: ")
fileSelShow = tk.Label(top, textvariable=cspFile)
openFileButt = tk.Button(top, text="Select File", width=10, command=getFilePath)
submitButt = tk.Button(top, text="Submit!", width=10, command=closeWindow)
fileSel.place(x=5, y=1)
fileSelShow.place(x=45, y=20)
openFileButt.place(x=6, y=47)
submitButt.place(x=95, y=47)
top.geometry("180x75+750+450")
top.attributes("-toolwindow", True)
top.resizable(width=False, height=False)
top.mainloop()
return cspFilePath.get()
def cspLogin(path):
# Configuring Browser
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# options.headless = True
# Settig chromedriver Path
wd_path = path + "\chromedriver.exe"
csp_user = 'user'
csp_pass = 'pass'
brwsr = webdriver.Chrome(executable_path=wd_path, options=options)
# Open CSP
brwsr.get('web')
usr_input = brwsr.find_element_by_xpath('//*[@id="username"]')
usr_input.clear()
usr_input.send_keys(csp_user)
pass_input = brwsr.find_element_by_xpath('//*[@id="password"]')
pass_input.clear()
pass_input.send_keys(csp_pass)
# inserting captcha
getCaptchaImg(brwsr, path)
captcha_code = captchaInterface(path)
captcha_input = brwsr.find_element_by_xpath(
'//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[2]/td[2]/table/tbody/tr[3]/td[2]/input')
captcha_input.clear()
captcha_input.send_keys(captcha_code)
brwsr.find_element_by_xpath(
'//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[3]/td[2]/table/tbody/tr/td/a').click()
brwsr.find_element_by_xpath('//*[@id="navigation"]/table[4]/tbody/tr[2]/td/a').click()
brwsr.find_element_by_xpath('//*[@id="emailAdd"]').send_keys(csp_user)
brwsr.find_element_by_xpath('//*[@id="batchType"]/option[3]').click()
cspFile = submitFileInterface()
subFile = brwsr.find_element_by_xpath(
'//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[2]/td[2]/table/tbody/tr/td[1]/label/input')
subFile.send_keys(cspFile)
goButton = '//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[5]/td[2]/table/tbody/tr[2]/td[1]/a'
brwsr.find_element_by_xpath(goButton).click()
brwsr.quit()
# cspLogin(webDriv_path)
这是我尝试使用它的模块
import os
from pathlib import Path
import time
import getpass
import json
from shutil import copy2
import datetime as dt
from csp_dwnld import *
# Program created to test if a python based windows app can work with an external txt
base_path = Path(__file__).parent.__str__()
dirTxtLoc = base_path + "\settings.txt" # Directory is stored in a txt file
os.system("mode con: cols=120")
def showMenu():
topRefresh("*** Welcome to quoting assistant *** ".center(120))
wDir = loadFolderPath(dirTxtLoc) # Working Directory
checkFiles()
print("1 - Set/Change Directory folder Location")
print("2 - Set csp login")
print("3 - Create new folder")
print("4 - Submit csv to csp")
print("\n\n\n\n")
print("9 - Help")
print("0 - Exit\n")
option = input("Select an option: ")
print('\n')
# while option != "0":
optSelection(option, wDir, base_path)
print("\n")
# showMenu()
def optSelection(option, path, alt_path):
if option == "1":
setDirectory()
elif option == "2":
setCsp()
elif option == "3":
createFolder(path)
elif option == "4":
cspLogin(alt_path)
elif option == "9":
optHelp()
elif option == "0":
os.system("exit")