In the use of Python tkinter Set a specific size for the window , Use Tk() Class variable geometry() function .
from tkinter import *
gui = Tk()
gui.geometry("widthxheight")
among ,width and height It should be replaced by integer numbers representing the width and height of the window, respectively . Pay attention to the geometry() Of width and height There is one between the variables x.
Be careful : Notice the size of the window width x height The title window does not contain the title window .
In this example , We will use geometry() The way to give Tk() Window set a fixed 500 ride 200 Size .
from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("500x200")
gui.mainloop()
Execution and output :
You can see , When the change procedure is being implemented , Will open a geometry() Function to define the size of the window GUI window .
Now let's change the offer to geometry() The width and length of the function , such as 300 ride 300.
from tkinter import *
gui = Tk(className='Python Examples - Window Size')
# set window size
gui.geometry("300x300")
gui.mainloop()
Execution and output :
In this section, we will learn how to use the tkinter Of GUI Program setting window size .
We will use Python Of tkinter Library to implement Python GUI Button in .
Add a button to tkinter The syntax of the window is as follows :
mybutton = Button(master, option=value)
mybutton.pack()
among master Is for the window you want to add to .tkinter The library provides different options for button constructors to change their appearance .
Options | value |
---|---|
text | Text display of button label |
width | Set the width of the button |
height | Set the height of the button |
bg | Sets the background color of the button |
fg | Set the font color of the button label |
activebackground | Set the background color after the button is clicked |
activeforeground | Set the font color of the button label after the button is clicked |
command | The function to call when the button is clicked |
font | Set the font size of the button label 、 Format |
image | Set button image |
In the next example , We're going to create one tkinter Button .
from tkinter import *
gui = Tk(className='Python Examples - Button')
gui.geometry("500x200")
# create button
button = Button(gui, text='My Button', width=40, height=3, bg='#0052cc', fg='#ffffff', activebackground='#0052cc', activeforeground='#aaffaa')
# add button to gui window
button.pack()
gui.mainloop()
Execution and output :
In this section, we learned how to use tkinter Library to create a GUI Button .
When one tkinter When the button is pressed , You can use command Property to call a function . Assign the function name to which you want the button to be called when it is clicked command Attribute is enough .
Here is the pseudo code for calling a function when the button is clicked :
def someFunction:
function body
tkWindow = Tk()
button = Button(tkWindow, command=someFunction)
Or you can assign it after defining the button command:
def someFunction:
function body
tkWindow = Tk()
button = Button(tkWindow)
button['command'] = someFunction
from tkinter import *
from tkinter import messagebox
tkWindow = Tk()
tkWindow.geometry('400x150')
tkWindow.title('Defonds.net Tkinter Example')
def showMsg():
messagebox.showinfo('Message', 'You clicked the Submit button!')
button = Button(tkWindow, text='Submit', command=showMsg)
button.pack()
tkWindow.mainloop()
Execution and output :
We need to pay attention when we call the function after we provide the user click button. :
To change the font properties , Such as font series 、 font size 、 Font thickness, etc , have access to tkinter.font package .
In the program , introduce tkinter.font as font And provide variables to font.Font().
In the following example , We will use family Name the parameter to font.Font() To change tkinter The font series of buttons .
from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(family='Helvetica')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()
gui.mainloop()
Execution and output :
If not provided font, This button will look like this :
We can also pass named parameters text to font.Font() To change tkinter The font size of the text in the button .
from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(size=30)
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font'] = myFont
# add button to gui window
button.pack()
gui.mainloop()
Execution and output :
We can also pass named parameters weight to font.Font() To change tkinter The font thickness of the text in the button .
from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples - Button')
gui.geometry('500x200')
# define font
myFont = font.Font(weight='bold')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()
gui.mainloop()
Execution and output :
We can package and pass the above settings to font.Font().
from tkinter import *
import tkinter.font as font
gui = Tk(className='Python examples = Button')
gui.geometry('500x200')
# define font
myFont = font.Font(family='Helvetica', size=20, weight='bold')
# create button
button = Button(gui, text='My Button', bg='#0052cc', fg='#ffffff')
# apply font to the button label
button['font']=myFont
# add button to gui window
button.pack()
gui.mainloop()
Execution and output :
We changed the font family to Courier Then run the program again :
myFont = font.Font(family='Courier', size=20, weight='bold')
Execution and output :
In this section , We use detailed examples , Demonstrates how to change tkinter The font series of buttons 、 Font size and font thickness .
Reference material