0% found this document useful (0 votes)
11 views4 pages

Python GUI Programming Examples

The document contains practical questions and solutions for Python GUI programming using Tkinter. It includes examples for creating buttons to change background colors, setting label text from an entry widget, displaying text in a message box, drawing on a canvas, and using radio buttons and check buttons. Additionally, it covers creating a list box with functionality to add, delete, and display items.

Uploaded by

radwanaeef
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Python GUI Programming Examples

The document contains practical questions and solutions for Python GUI programming using Tkinter. It includes examples for creating buttons to change background colors, setting label text from an entry widget, displaying text in a message box, drawing on a canvas, and using radio buttons and check buttons. Additionally, it covers creating a list box with functionality to add, delete, and display items.

Uploaded by

radwanaeef
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python GUI Programming-Practical Questions

1. Create 3 buttons named RED, BLUE and GREEN. Click on the buttons to change
background colour of window.

from tkinter import *


def redbg( ):
[Link](bg="RED")
def greenbg( ):
[Link](bg="GREEN")
def bluebg( ):
[Link](bg="BLUE")

window=Tk()
[Link]("400x400")
b1=Button(window, text="RED" ,command=redbg)
b2=Button(window, text="GREEN" ,command=greenbg)
b3=Button(window, text="BLUE" ,command=bluebg)

[Link]()
[Link]()
[Link]()
[Link]( )

2. Create a single line text input widget and a label. On clicking a button set the
label text as the text input through the entry widget.

from tkinter import *

def setlabel( ):
labeltext=[Link]()
[Link](text=labeltext)
window=Tk()
[Link]("400x400")
l=Label(window)
e=Entry(window,fg="RED",width=30)
b=Button(window, text="click" ,command=setlabel)
[Link]()
[Link]()
[Link]()

[Link]( )
3. Create an entry widget and input a text. On clicking on a button show the
entered text in a message box.

from tkinter import *


from tkinter import messagebox
def display():
txt=[Link]()
[Link]("Title",txt)

window=Tk()
[Link]("400x400")
e=Entry(window,width=30)
b=Button(window,text="Click",command=display)
[Link]()
[Link]()
[Link]( )
4. Create a canvas and draw a straight line on it.

from tkinter import*


window=Tk()
[Link]("400x400")
c=Canvas(window,bg="BLUE", height=300,width=400)
c.create_line(0,0,200,200,fill="RED")
[Link]()
[Link]( )
[Link] a program to display the text associated with a radio button in a message
box.

from tkinter import *


from tkinter import messagebox
def dis():
s=[Link]()
[Link]("title",s)

window = Tk()
[Link]("400x400")
v=StringVar()
R1=Radiobutton(window,text="Male",variable=v,value="male")
R2=Radiobutton(window,text="Female",variable=v,value="female")
l=Label(window)
b=Button(window,text="Click",command=dis)
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

[Link] a program to trace the text associated with the selected check button(s)

from tkinter import *

def setval():
if ([Link]() == 1) and ([Link]() == 0):
[Link](text="You Love Music")
elif ([Link]() == 0) and ([Link]() == 1):
[Link](text="You Love Dance")
elif ([Link]() == 1) and([Link]() == 1):
[Link](text="You Love Music&Dance")

else:
[Link](text="You Love neither" )
window = Tk()
[Link]("400x400")
l = Label(window, width=20)
[Link]()
var1 =IntVar()
var2 = IntVar()

c1 = Checkbutton(window, text="Music",variable=var1, onvalue=1, offvalue=0,


command=setval)
c2 = Checkbutton(window, text="Dance",variable=var2, onvalue=1, offvalue=0,
command=setval)
[Link]()
[Link]()
[Link]()
7. Create a List box and add 3 buttons to add items to list,delete items from the list
and to print the list items.

from tkinter import *


def del1():
[Link](ANCHOR)
def ins():
item=[Link]()
[Link](END,item)
def disp():
print([Link](0,END))

window = Tk()
[Link]("800x600")
lbl = Label(window,text = "A list of favourite countries..")
e=Entry(window)
[Link]()
listbox = Listbox(window,selectmode=EXTENDED)
[Link](1,"India")
[Link](2, "USA")
[Link](3, "Japan")
#this button will delete the selected item from the list
btn1=Button(window, text = "delete",command=del1)
#this button will insert the item in the Entry to the end of listbox
btn2=Button(window, text = "insert",command=ins)
#this button will print a tuple containing all items in the list

btn3=Button(window, text = "display",command=disp)


[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

Common questions

Powered by AI

Tkinter's Canvas widget allows drawing and manipulation of graphical content, such as lines, shapes, and text. By using methods like 'create_line()', you can draw straight lines by specifying coordinates, as seen in creating a red line from coordinates (0,0) to (200,200). This widget offers flexibility for placing and customizing visual elements on the GUI, making it a powerful tool for developing applications with custom graphics or visual feedback within the GUI window .

Tkinter demonstrates the Observer design pattern, a core principle of event-driven programming, where events (user interactions) trigger updates in the application. Widgets like buttons and checkboxes in tkinter act as subjects that can notify observers (functions) when an event occurs, such as a button click. This separation of concerns allows for modular code, where the GUI logic is decoupled from application logic. Event handlers or callback functions are defined separately and orchestrated through these patterns, fostering a responsive and dynamic user interface. This approach exemplifies fundamental software design principles of modularity and separation of concerns, making it easier to manage complexity within GUI applications .

Tkinter provides widgets like Entry and Label to handle user inputs. An Entry widget can be used to receive input from users, which you can then extract using the 'get()' method. This input can be displayed in real-time by setting it as the text for a Label widget using 'label.config(text=input_text)'. To update the label dynamically, you can bind this updating process to the command of a button that retrieves the input text and updates the label, ensuring that any entered text is immediately reflected when the button is clicked .

Tkinter manages events in GUI applications including complex components like message boxes (MessageBox) by invoking them through methods such as 'showinfo()' which display information to the user. Message boxes can serve multiple purposes such as confirmation dialogs, alerts, and notifications, enhancing how applications communicate with users. They are commonly used in scenarios where critical feedback or confirmations are required, such as validating form submissions, notifying of successful actions, or cautioning against potential errors. Being modal, they demand acknowledgement before users can proceed, ensuring critical information is not overlooked in process-driven applications .

The Listbox widget in tkinter allows users to manipulate collections of data in an intuitive graphical format. It supports item addition, deletion, and retrieval, providing methods such as 'insert()', 'delete()', and 'get()' to manage list items. This reflects a user-centric design by enabling interaction through GUI elements rather than code modification or command line input, enhancing accessibility and usability. By associating buttons with functions to manipulate the Listbox, such as inserting data from an Entry field or displaying current list items, tkinter applications can support dynamic data handling that responds to user actions in real time .

Tkinter facilitates management and manipulation of textual data through various widgets such as Entry for input, Label for display, and MessageBox for dialog displays. These widgets enhance user interaction by providing immediate feedback and options to manipulate data. For instance, text entered in an Entry widget can be displayed in a Label upon a button click by retrieving the text with 'Entry.get()' and updating the label text through 'Label.config(text)'. Additionally, tkinter's MessageBox can display input or selection from widgets like RadioButtons, enabling user interaction to yield visible application state changes, such as displaying selected radio button text in a message box .

Conditional logic in tkinter applications is applied using control flow statements like 'if-elif-else' in callback functions. For example, Checkbuttons can be used where each button is tied to a variable that holds the state (checked/unchecked). When the user alters the selection, a function can be called that evaluates these states and executes logic to provide appropriate feedback. For example, a program might display messages like "You Love Music" or "You Love Dance" based on whether a 'Music' or 'Dance' Checkbutton is selected. This kind of dynamic conditional feedback harnesses tkinter's callback mechanism to update Labels based on multi-state inputs .

Connecting multiple widgets for integrated behavior in tkinter can be achieved through callback functions and shared variables. By using shared state or variables, such as StringVar or IntVar, widgets like RadioButtons, Checkbuttons, and Labels can share and react to the same data, creating synchronized behaviors. For instance, selecting a RadioButton can automatically update a Label or trigger further dynamic changes in the interface. Additionally, combining Entry widgets with buttons to update other widget settings demonstrates integration. This interconnectedness creates a more cohesive and user-friendly interface, enhancing interaction by reflecting real-time changes consistently across the application .

Tkinter's RadioButton widget allows users to select exactly one option among mutually exclusive choices, ensuring single selection from a group, which is ideal for scenarios like selecting gender or a setting preference. It differs from Checkbuttons, which allow multiple selections since they aren't mutually exclusive. RadioButtons capture a specific choice with a shared variable reflecting the currently selected button, simplifying choice management in the application logic. This makes them ideal for structured inquiries, while Checkbuttons are better for independent option selections, such as preferences or permission settings. Both widgets contribute to efficient user input collection, offering flexibility in designing intuitive user interfaces .

The tkinter library allows developers to dynamically alter a GUI application's appearance in various ways through event-driven programming. For instance, you can create buttons with specific commands that alter window properties such as its background color. By defining functions that configure the window's properties, such as changing the background color with 'window.config(bg="COLOR")', and binding these functions to button commands, you can allow users to interactively change the GUI's appearance. This capability is demonstrated in programs that use buttons to set different background colors, such as red, green, and blue .

You might also like