通过tkinter实现百度搜索

"""
百度搜索
已打包成 百度搜索.exe
"""
import tkinter

import win32api
from selenium.webdriver import Chrome

entry = None


def callback():
    global entry
    keywords = entry.get()
    if not keywords:
        win32api.MessageBox(0, '请输入搜索关键字', '提示', 0)
        return
    chrome = Chrome()
    chrome.get('https://www.baidu.com/')
    chrome.find_element_by_id('kw').send_keys(keywords)
    chrome.find_element_by_id('su').click()

    # bilibili关键字搜索
    # chrome.get('https://www.bilibili.com/')
    # chrome.find_element_by_xpath('//form[@id="nav_searchform"]/input').send_keys(keywords)
    # chrome.find_element_by_xpath('//div[@class="nav-search-btn"]/button').click()


def main():
    global entry
    tk = tkinter.Tk()
    # tk.resizable(width=False,height=False)  # 固定窗体大小?无效
    tk.title('百度搜索')

    # 1.设置窗体居中
    # screenwidth = tk.winfo_screenwidth()  # 获取屏幕宽度
    # screenheight = tk.winfo_screenheight()  # 获取屏幕高度
    # # 计算窗体大小,位置参数,width,height:窗体宽高
    # width = 100
    # height = 50
    # size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    # tk.geometry(size)  # 设置窗体位置为屏幕居中

    # 2.设置窗体右下角,无效
    # screenwidth = tk.winfo_screenwidth()  # 获取屏幕宽度
    # screenheight = tk.winfo_screenheight()  # 获取屏幕高度
    # print(screenwidth,screenheight)
    # # 计算窗体大小,位置参数,width,height:窗体宽高
    # width = 100
    # height = 50
    # size = '%dx%d+%d+%d' % (width, height, (screenwidth - width), (screenheight - height))
    # tk.geometry(size)  # 设置窗体位置为屏幕右下角

    # 获取窗体x,y
    # tk.update()
    # print(tk.winfo_x())
    # print(tk.winfo_y())

    tk.geometry('+0+0')  # 固定屏幕左上角
    # tk.geometry('+1440+770')

    entry = tkinter.Entry(tk)
    entry.pack()

    button = tkinter.Button(tk, text='百度一下', command=callback)
    button.pack()

    tk.mainloop()


if __name__ == '__main__':
    main()

 

你可能感兴趣的:(Python)