Found 10476 Articles for Python

How to call a function using the OptionMenu widget in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 14:28:55

5K+ Views

Let's take an example and see how to call a function using OptionMenu widget in Tkinter. In the example, we will use a StringVar object and call its get() method. A StringVar object in Tkinter can help manage the value of a widget.We will create an OptionMenu widget and fill it with a list of strings. When the user selects an option, it will invoke a function which in turn will print the selected option as a label.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a set of ... Read More

How to get the longitude and latitude of a city using Python?

Kiran Kumar Panigrahi
Updated on 29-Aug-2023 03:28:47

32K+ Views

To get the longitude and latitude of a city, we will use the geopy module. geopy uses third-party geocoders and other data sources to locate the coordinates of addresses, cities, countries, etc.First of all, make sure the geopy module is installed −pip install geopyIn the following example, we will use the Nominatim geocoder to find the longitude and latitude of the city "Hyderabad".Steps −Import Nominatim geocoder from geopy module.Initialize the Nominatim API and use the geocode method to get the location of the input string.Finally, get the latitude and longitude of the location by location.latitude and location.longitude.Example 1# Import the ... Read More

How to use a StringVar object in an Entry widget in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:51:25

5K+ Views

A StringVar object in Tkinter can help manage the value of a widget such as an Entry widget or a Label widget. You can assign a StringVar object to the textvariable of a widget. For example, data = ['Car', 'Bus', 'Truck', 'Bike', 'Airplane'] var = StringVar(win) my_spinbox = Spinbox(win, values=data, textvariable=var)Here, we created a list of strings followed by a StringVar object "var". Next, we assigned var to the textvariable of a Spinbox widget. To get the current value of the Spinbox, you can use var.get().ExampleThe following example demonstrates how you can use a StringVar object in an ... Read More

How to set a default string value on a Tkinter Spinbox?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:48:55

3K+ Views

To set a default string value on a Tkinter Spinbox, we will have to use the set method. Let us take an example and see how to create a spinbox with a set of string values and then set a default string.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a set of strings and save it in a variable, data.Next, use the StringVar() constructor to create a StringVar object. It helps to manage the value of a widget, which is Spingbox in this case. If you don't pass ... Read More

How to draw a line following mouse coordinates with tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:46:39

3K+ Views

To draw a line following mouse coordinates, we need to create a function to capture the coordinates of each mouse-click and then draw a line between two consecutive points. Let's take an example and see how it can be done.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a user-defined method "draw_line" to capture the x and y coordinates of each mouse click. Then, use the create_line() method of Canvas to draw a line between two consecutive points.Bind the left-click of the mouse with the draw_line method.Finally, run the ... Read More

How to place the text at the center of an Entry box in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:44:13

10K+ Views

To put a border around a Frame in Tkinter, we have to use the highlightbackground and highlightthickeness parameters while creating the Frame. Let's take an example and see how to use these two parameters.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a frame with Frame() method. Highlight the border of the frame with a color, highlightbackground="blue". Then, set the thickness of the border, highlightthickness=2.Next, create some widgets inside the frame. In the example, we have placed four checkbuttons and a button inside the frame.Finally, run the mainloop of ... Read More

How to put a border around a Frame in Python Tkinter?

Kiran Kumar Panigrahi
Updated on 28-Aug-2023 11:56:44

36K+ Views

To put a border around a Frame in Tkinter, we have to use the highlightbackground and highlightthickeness parameters while creating the Frame. Let's take an example and see how to use these two parameters.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry() method.Create a frame with Frame() method. Highlight the border of the frame with a color, highlightbackground="blue". Then, set the thickness of the border, highlightthickness=2.Next, create some widgets inside the frame. In the example, we have placed four checkbuttons and a button inside the frame.Finally, run the mainloop of ... Read More

Copy from clipboard using Python and Tkinter

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:40:07

7K+ Views

To copy from clipboard, we can use the clipboard_get() method of Tkinter. Let's take an example and see how to get the data from the clipboard and display it on a Tkinter window.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Next, call clipboard_get() to get the text from the clipboard and store the data in a variable "cliptext".Create a label to the display the clipboard text. Pass cliptext as text, "text=cliptext".Finally, run the mainloop of the application window.Example# Import the tkinter library from tkinter import * # Instance ... Read More

Display the Host Name and IP Address on a Tkinter Window

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:37:35

969 Views

To obtain a user's IP address, we can use Python's native networking interface, socket. First of all, we need to query the device's host name and then get its associated IP address.In this example, we will use the socket library to get the host name and IP address and print the details on two labels.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Next, use the gethostname() method of socket library to get the host name and store it in a variable "hostname".Then use the gethostbyname() method and pass the ... Read More

How to draw a dashed line on a Tkinter canvas?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 13:35:43

4K+ Views

To draw a dashed line on a Tkinter canvas, we can use the dash parameter of create_line() method.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a Canvas widget and set its height and width.Next, use the create_line function and pass the coordinates of the line (x1, y1) and (x2, y2).To get a dashed line, use the dash parameter dash=(5, 1) for 5px dash followed by 1px space.You can set the color and width of the dashed lines using the fill and width parameters.Finally, run the mainloop of the ... Read More

Advertisements