Found 10476 Articles for Python

How to run an infinite loop in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:48:38

8K+ Views

To run an infinite loop in Tkinter, we will use the after method to call a method recursively after a specified time period until the user decides to stop the loop. Let's take a simple example and see how to start and stop an infinite loop.Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using win.geometry method.Next, create a user-defined function "infinite_loop" which will call itself recursively and print a statement on the window.Define two more user-defined functions, start() and stop(), to control the infinite_loop. Define a global variable "condition". Inside start(), ... Read More

How can I determine the position of a Toplevel in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:45:08

2K+ Views

To place objects in the middle of a frame, we can use the place method. Let's take an example and see how it's done.Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using win.geometry method.Next, create a button and label it.Set the position of the buttons using the place method by supplying the x and y coordinate values.Place the center of the widget at a relative x and y position of 0.5 of button widget (relx=0.5, rely=0.5). Set the anchor at the center by supplying "anchor=CENTER"Finally, run the mainloop of the application ... Read More

How to place objects in the middle of a frame using tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:40:58

4K+ Views

To place objects in the middle of a frame, we can use the place method. Let's take an example and see how it's done.Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using win.geometry method.Next, create a button and label it.Set the position of the buttons using the place method by supplying the x and y coordinate values.Place the center of the widget at a relative x and y position of 0.5 of button widget (relx=0.5, rely=0.5). Set the anchor at the center by supplying "anchor=CENTER"Finally, run the mainloop of the application ... Read More

How do I position the buttons on a Tkinter window?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:38:26

13K+ Views

To set the position of a button, we use the place method of button widget. The place method takes the x and y coordinates of the button.Steps −Import the required libraries and create an instance of tkinter frame.Set the size of the frame using win.geometry method.Next, create multiple buttons and name them "Button-1", "Button-2", etc.Set the position of the buttons using the place method by supplying the x and y coordinate values.Finally, run the mainloop of the application window.Example# Import the Tkinter library from tkinter import * from tkinter import ttk # Create an instance of Tkinter frame win ... Read More

How to bind a Tkinter event to the left mouse button being held down?

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

3K+ Views

To bind a Tkinter event to the left mouse button being held down, we can take the following steps −Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define an event handler "handler1" to print a statement when the mouse is moved with the left button being held down.Define another event handler "handler2" to print a statement when the mouse button is released.Use the bind method to bind with handler1.Use the bind method again to bind with hander2.Finally, run the mainloop of the application window.Example# Import required libraries from tkinter import * # ... Read More

How to save the contents of a Textbox in Tkinter?

Kiran Kumar Panigrahi
Updated on 26-Oct-2021 12:32:06

7K+ Views

To save the contents of a Textbox in Tkinter, we can take the following steps −Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define a user-defined method "open_text" to open a text file in "read" mode. Read the contents of the text file and save it in a variable called "content". Then, use the "insert" method to insert the contentin a Textbox.Next, define another user-defined method called "save_text" and in it, use the "write" method to save the contents of the textbox in the text file.Create a text widget using the Text method with specified ... Read More

How to make a new folder using askdirectory dialog in Tkinter?

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

3K+ Views

To make a new folder using askdirectory dialog in Tkinter, we can take the following steps −Import the required modules. filedialog module is required for askdirectory method. os module is required for makedirs method.Create an instance of tkinter frame.Set the size of the frame using win.geometry method.Define a user-defined method "create_subfolder". Inside the method, call filedialog.askdirectory to select a folder and save the path in a variable, source_path.We can use askdirectory method of filedialog to open a directory. Save the path of the selected directory in a 'path' variable.Then, use os.path.join and makedirs to create a sub-folder inside the parent ... Read More

Program to find coefficients of linear equations that has only one solution in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:11:57

504 Views

Suppose we have a value n, we have to find the number of pairs (a, b) [a < b], that exist such that the equation a*x + b*y = n, has at least one solution.So, if the input is like n = 4, then the output will be 2 because the valid pairs are (1, 2) and (1, 3).To solve this, we will follow these steps −Define a function divisors_gen() . This will take ndivs := a list of lists of size n+1. And each inner list is holding 1divs[0] := a list with only one element 0for i in ... Read More

Program to find number m such that it has n number of 0s at end in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:08:24

124 Views

Suppose we have a number n. We have to find smallest number m, such that factorial of m has at least n number of 0s.So, if the input is like n = 2, then the output will be 10 because 10! = 3628800 and 9! = 362880, minimum number with 2 zeros is 10.To solve this, we will follow these steps −Define a function count_fives() . This will take ncnt := 0while n > 0, don := floor of (n / 5)cnt := cnt + nreturn cntFrom the main method, do the following −left := 1right := 5^24while right - ... Read More

Program to find hoe many children will get candies while distributing them maintaining the rules in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:05:08

319 Views

Suppose we have k number of candies. We have to distribute them among children. Now there are some rulesith child will get i^2 number of candiesany children at index i will not get any candy until all children from index 1 to i-i are servedIf ith children does not get i^2 number of candies, then that is not a valid serve.So, if the input is like k = 20, then the output will be 3 because, first one will get 1, second one will get 2^2 = 4, third one will get 3^2 = 9, but fourth one needs 4^2 ... Read More

Advertisements