GUI-Programming Articles

Page 6 of 25

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

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 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
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 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
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 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

Display the Host Name and IP Address on a Tkinter Window

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 1K+ 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
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 5K+ 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

How to align checkbutton in ttk to the left side?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 5K+ Views

To align the checkbuttons to left, you can use the anchor parameter and set it to "w" (west). Let's take an example and see how to do it.Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Create a LabelFrame to collect the checkbuttons in a group.Next, create a checkbutton inside the LabelFrame and set its anchor to the West. anchor='w'.Similarly, create three more checkbuttons with their anchor set at West. It will align all the checkbuttons to the left.Finally, run the mainloop of the application window.Examplefrom tkinter import * ...

Read More

How to exit from Python using a Tkinter Button?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 25K+ Views

To exit from Python using a Tkinter button, you can follow the steps given below −Steps −Import the tkinter library and create an instance of tkinter frame.Set the size of the frame using geometry method.Define a function close() to close the window. Call the method win.destroy() inside close().Next, create a button and call the close() function.Finally, run the mainloop of the application window.Example# Import the library from tkinter import * # Create an instance of window win = Tk() # Set the geometry of the window win.geometry("700x350") # Title of the window win.title("Click the Button to Close ...

Read More

Tkinter-How to get the current date to display in a tkinter window?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 8K+ Views

To display the current date in tkinter window, we will use the datetime library.date = dt.datetime.now()Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using geometry method.Call datetime.now() and store the value in a variable "date".Next, create a label to display the date. In the text parameter of the label, pass the date value and format the data as text=f"{date:%A, %B %d, %Y}".%A – Day of the week, full name%B – Full month name%d – Day of the month%Y – Year with century as a decimal numberFinally, run the mainloop of the ...

Read More

Creating an automatically maximized tkinter window

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 3K+ Views

There are two different ways in which we can get an automatically maximized window in Tkinter.We can use the state() method of Tkinter and invoke it with the attribute "zoomed".root.state("zoomed")The second approach is to use the attributes method of Tkinter with the parameter "-fullscreen" and set it to True.By default, Tkinter creates a window of a predefined size. The dimensions of the window can be customized using the geometry method. For example, root.geometry("700 x 350") Example 1# Import the required libraries from tkinter import * # Create an instance of tkinter frame root=Tk() # Create a label Label(root, ...

Read More

Tkinter - How to put an outline on a canvas text

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 26-Oct-2021 3K+ Views

The create_text method of Canvas widget in Tkinter doesn't have an attribute like "outline" or "border" to set an outline around a text object. So, to put an outline on a canvas text, you can follow the steps given below −Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using root.geometry method.Create a Canvas widget and set its height and width. Also, set its background color with background="white".Next, create a text object inside the Canvas using create_text() method. Set the font and color of the text as shown in the example.Get the ...

Read More
Showing 51–60 of 242 articles
« Prev 1 4 5 6 7 8 25 Next »
Advertisements