Home > other >  running tkinter module unsuccesfully
running tkinter module unsuccesfully

Time:10-20

I have a module that doesn't define any classes or functions. Code is only written in an imperative manner, starting at the top and working its way down.

I want to use the tkinter module to call it, so I've added a GUI to new module so that it run as an app rather than from a command prompt.

I attempted to import it but failed. Then in new module I defined all readyModule as a function without argument.

import openpyxl
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd
from openpyxl import load_workbook

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Your App For Crawling")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

feet = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=1, row=1, sticky=(W, E),ipadx=100,padx=33)

def crawler():
    varInp= input("Url (enter) :")...#module code

ttk.Button(mainframe, text="scrape", command=crawler()).grid(column=3, row=3, sticky=W)
feet_entry.focus()
root.bind("<Return>", crawler())

root.mainloop()

But, when I click run, it starts to ask input value which is inside defined function. I think I have written enough code for tkinter to open userinterface

...edited, If I add root.mainloop() up than def crawling() function it opens up in that manner that I want, but when I close GUI window, on cmd it again asks abput input url

CodePudding user response:

You do 3 common mistakes in just 2 lines of code. Avoid to use blocking calls such as input you have tkinter.Entry for this, you can retrieve the input either with feet.get() or with feet_entry.get(). command=crawler() the parenthesis will run the function immediately, adjust to command=crawler, tkinter will wrap this in a tcl procedure that runs when the button is clicked or otherwise the command is invoked. The last common mistake is that you use constructor(master, **options).geometrymethod.(**options). Rather store the constructed widget in a variable and use the geometrymethod on it. You might want to change the state or reposition the widget with a different manager, all this is easier don with a stored reference.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Your App For Crawling")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky='nswe')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

feet = tk.StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=1, row=1, sticky=(tk.W, tk.E),ipadx=100,padx=33)
feet_entry.focus()

def crawler():
    varInp= feet.get()

button = ttk.Button(mainframe, text="scrape", command=crawler)
button.grid(column=3, row=3, sticky=tk.W)
root.bind("<Return>", crawler)

root.mainloop()
  • Related