how can make the button enable and disable in tkinter? [closed] - python-3.x

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to see that when the user adds something to the entry field my button gets enabled and then when the user deletes that entry my button gets disabled

A way to disable a button is to use this code:
button = tkinter.Button(root, text='enable/disable'))
def switchButtonState():
if (button['state'] == tk.NORMAL):
button['state'] = tk.DISABLED
else:
button['state'] = tk.NORMAL
To get the input out of a text widget would mean that you would have to run this code:
root.after(1000, check_text_box) # I am pretty sure the 1000 is number of milliseconds
What the after function does is every 1000 milliseconds, it calls the check_text_box function
Here is the complete code to make it work:
def check_to_disable_enable():
text = text_widget_name.get(1.0, tkinter.END)
if text == '': # If the textbox is empty
button['state'] = tk.DISABLED # Disables button
else: # If the textbox is not empty
button['state'] = tk.NORMAL # Enables button
# Call this function when the frame comes up
root.after(100, check_to_disable_enable)
I realize that this may be a bit confusing and if it is please don't refrain from asking a question. If this answers your question, please mark it answered.

Related

How to properly display a picture in PyQt5? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am making a podcast application where I want to display channel icons. That's where I asked myself : How do I properly display a picture in PyQt5 ? I read about this:
label = QLabel()
pixmap = QPixmap('image.png')
label.setPixmap(pixmap)
It's often done that way, but I feel a bit, well, uneasy, about it. It feels hackish to me even though there is an extra method to do it like this. Is there any docs passage where the above way is defined as the official standard one ? If not, is there a better way ? Something like
lay = QVBoxLayout()
image = SomeWidget("image.png")
lay.addWidget(image)
All Qt subclasses of QObject (which includes all subclasses of QWidget, since it inherits from QObject) allow setting their Qt properties in the constructor as keyword arguments.
Since pixmap is a property of QLabel, you can create one directly with an image set with the following line:
image = QLabel(pixmap=QPixmap('image.png'))
If you are so bothered about that, then just create a convenience function or class:
def ImageWidget(path):
return QLabel(pixmap=QPixmap(path))
# or, alternatively
class ImageWidget(QLabel):
def __init__(self, path):
super().__init__(pixmap=QPixmap(path))

How to automate the command line for the input() in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to python and I am using idle 3.7.4. I want to automate command line to input() in python.
For example:
a = input("Please enter your name : ")
I want to add my name without manually typing it. I read about run() method under subprocess module, but I am not sure how to implement it.
Any helps are highly appreciated. Thanks.
you can use the mock library to do this with the builtins. For example, like this:
import mock
import builtins
def input_test():
ans = input("Please input your name :")
return f"your name is {ans}"
def test_input():
with mock.patch.object(builtins, 'input', lambda _: "okay"):
assert input_test() == 'your name is okay'

How to login a site using Python 3.7? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to write a code in python 3.7 that login a site.
For example, When I get username and password, the code should do the login process to the site.
I'm unsure of what you mean, if you're logging into a site or trying to process a login request, but I'm going to assume the former.
There's one way using Selenium. You can inspect element the site to find the id's for the input for username/password, then use the driver to send the information to those inputs.
Example:
from selenium import webdriver
username = "SITE_ACCOUNT_USERNAME"
password = "SITE_ACCOUNT_PASSWORD"
driver = webdriver.Chrome("path-to-driver")
driver.get("https://www.facebook.com/")
username_box = driver.find_element_by_id("email")
username_box.send_keys(username)
password_box = driver.find_element_by_id("pass")
password_box.send_keys(password)
try:
login_button = driver.find_element_by_id("u_0_8")
login_button.submit()
except Exception as e:
login_button = driver.find_element_by_id("u_0_2")
login_button.submit()
This logs into facebook using the chromedriver.

how can i get all the urls in chrome network by using python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I get all the Request URL in chrome network
Right click -> Copy -> Copy all as HAR
Then you can import it like this:
import json
obj = json.loads(
'''
<paste here>
'''
)
You can then get urls = [ entry['request']['url'] for entry in obj['log']['entries'] ]
You may need to replace \" with \\" in your text editor for it to compile.

(PYTHON) Manipulating certain portions of URL at user's request [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
The download link I want to manipulate is below:
http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/USWC/6km/hourly/RTV/HFRADAR,_US_West_Coast,_6km_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&north=47.20&west=-126.3600&east=-123.8055&south=37.2500&horizStride=1&time_start=2015-11-01T00%3A00%3A00Z&time_end=2015-11-03T14%3A00%3A00Z&timeStride=1&addLatLon=true&accept=netcdf
I want to make anything that's in bold a variable, so I can ask the user what coordinates and data set they want. This way I can download different data sets by using this script. I would also like to use the same variables to name the new file that was downloaded ex:USWC6km20151101-20151103.
I did some research and learned that I can use the urllib.parse and urllib2, but when I try experimenting with them, it says "no module named urllib.parse."
I can use the webbrowser.open() to download the file, but manipulating the url is giving me problems
THANK YOU!!
Instead of urllib you can use requests module that makes downloading content much easier. The part that makes actual work is just 4 lines long.
# first install this module
import requests
# parameters to change
location = {
'part': 'USWC',
'part2': '_US_West_Coast',
'km': '6km',
'north': '45.0000',
'west': '-120.0000',
'east': '-119.5000',
'south': '44.5000',
'start': '2016-10-01',
'end': '2016-10-02'
}
# this is template for .format() method to generate links (very naive method)
link_template = "http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/{part}/{km}/hourly/RTV/\
HFRADAR,{part2},_{km}_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&\
north={north}&west={west}&east={east}&south={south}&horizStride=1&\
time_start={start}T00:00:00Z&time_end={end}T16:00:00Z&timeStride=1&addLatLon=true&accept=netcdf"
# some debug info
link = link_template.format(**location)
file_name = location['part'] + location['km'] + location['start'].replace('-', '') + '-' + location['end'].replace('-', '')
print("Link: ", link)
print("Filename: ", file_name)
# try to open webpage
response = requests.get(link)
if response.ok:
# open file for writing in binary mode
with open(file_name, mode='wb') as file_out:
# write response to file
file_out.write(response.content)
Probably the next step would be running this in loop on list that contains location dicts. Or maybe reading locations from csv file.

Resources