下面是小编给大家带来的Python写的Tkinter程序屏幕居中方法(共含5篇),以供大家参考,我们一起来看看吧!同时,但愿您也能像本文投稿人“在落星田看月亮”一样,积极向本站投稿分享好文章。
这篇文章主要介绍了Python写的Tkinter程序屏幕居中方法,Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口,需要的朋友可以参考下
本文适用场景:想用Tkinter开发界面程序并屏幕居中,但没找到相应的API,
这两天玩了玩Tkinter,感觉不错,就是屏幕居中这个问题在网上搜了很长时间也没
找到答案,最后没办法,看它的文档,用自己的方法实现了。
方法很土,就是获取初始化的窗体大小和屏幕大小,再通过计算得到大体值。
以下是代码:
代码如下:
#! /usr/bin/python
‘‘‘
File : screenCenter.pyw
Author : Mike
E-Mail : Mike_Zhang@live.com
‘‘‘
from Tkinter import *
rt = Tk
rt.resizable(False,False)
rt.title(“Screen center”)
rt.update() # update window ,must do
curWidth = rt.winfo_reqwidth() # get current width
curHeight = rt.winfo_height() # get current height
scnWidth,scnHeight = rt.maxsize() # get screen width and height
# now generate configuration information
tmpcnf = ‘%dx%d+%d+%d‘%(curWidth,curHeight,
(scnWidth-curWidth)/2,(scnHeight-curHeight)/2)
rt.geometry(tmpcnf)
rt.mainloop()
这篇文章主要介绍了Python编写屏幕截图程序方法,本文讲解使用开源程序pywin32实现屏幕截图和读取剪切板功能,需要的朋友可以参考下
正在编写的程序用的很多Windows下的操作,查了很多资料,看到剪切板的操作时,想起以前想要做的一个小程序,当时也没做,现在正好顺手写完。
功能:按printscreen键进行截图的时候,数据保存在剪切板里面,很不方便。比如游戏的时候截一个瞬间的图片,但你不能退出游戏保存图片,不方便多次截图。而我也不喜欢安装各种软件,所以准备写这个工具。
思路:一个是自定义快捷键,截图,保存。考虑到很可能各种冲突,取消。然后还是用按printscreen来截图,然后从剪切板读取图片数据,保存。想法是,先监听键盘按键,当printscreen按键时,读取剪切板内容,最后保存图片到指定位置。
1 监听键盘按键:从网上找到资料,安装pywin32,pyhook。链接:sourceforge.net/projects/pyhook/,sourceforge.net/projects/pywin32/。教程:sourceforge.net/apps/mediawiki/pyhook/index.php?title=PyHook_Tutorial。
2 读取剪切板内容,也是需要pywin32.文档在:[Pythonpath]Libsite-packagesPyWin32.chm,在线的:timgolden.me.uk/pywin32-docs/
文档中给出的几种格式都不是图片保存的数据,Google搜索“Standard Clipboard Formats”,链接:msdn.microsoft.com/en-us/library/windows/desktop/ff729168%28v=vs.85%29.aspx,所有的格式,主要就是1-17.
好在文档中有一个函数:GetPriorityClipboardFormat,可以返回剪切板中的格式,从一个迭代器中,
于是手动输入找到了,y有时为6,win32con.CF_TIFF,还会为2。当然这里直接用返回数据就行了,不需要知道是什么。
主要用到:
OpenClipboard,CloseClipboard,GetPriorityClipboardFormat,GetClipboardData,这几个函数文档都有介绍,主要说说CloseClipboard,官方文档上讲,不要在剪切板里放置对象后调用CloseClipboard。
3 保存图片:
到这里发现可以直接用PIL模块,直接解决问题,上面太曲折T_T。
可以直接使用ImageGrab.grab() 进行抓屏,或者使用ImageGrab.grabclipboard()从剪切板获取图像。
最后变成,监听按键,按下printscreen后,用pil截图保存。T_T 感觉还不如设置快捷键,这样应该少占用内存。
这篇文章主要介绍了Python中使用Tkinter模块创建GUI程序实例,本文给出了创建窗口、文本框、按钮等实例,需要的朋友可以参考下
使用Tkinter模块来创建简单的GUI程序,
Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。
例:
代码如下:
# This program displays an empty window.
import Tkinter
def main:
main_window = Tkinter.Tk()
Tkinter.mainloop()
main()
例2:
代码如下:
import Tkinter
class MyGUI:
def __init__(self):
# Create the main window widget.
self.main_window = Tkinter.Tk()
# Enter the Tkinter main loop.
Tkinter.mainloop()
# Create an instance of the MyGUI class.
my_gui = MyGUI()
例3:
代码如下:
# The program displays a label with text.
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
# Create a Label widget containing the text ‘Hello world‘
self.label = Tkinter.Label(self.main_window, text=‘Hello World!‘)
# Call the Label widget‘s pack method.
self.label.pack()
# Enter the Tkinter main loop.
Tkinter.mainloop()
# Create an instance of the MyGUI class.
my_gui = MyGUI()
例4:
代码如下:
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.label1 = Tkinter.Label(self.main_window,text=‘Hello World!‘)
self.label2 = Tkinter.Label(self.main_window,text=‘This is my GUI program.‘)
self.label1.pack()
self.label2.pack()
Tkinter.mainloop()
mygui = MyGUI()
例5:
代码如下:
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.label1 = Tkinter.Label(self.main_window,text=‘Hello World!‘)
self.label2 = Tkinter.Label(self.main_window,text=‘This is my GUI program.‘)
self.label1.pack(side=‘left‘)
self.label2.pack(side=‘left‘)
Tkinter.mainloop()
mygui = MyGUI()
例6:
代码如下:
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.top_frame. = Tkinter.Frame(self.main_window)
self.bottom_frame. = Tkinter.Frame(self.main_window)
self.label1 = Tkinter.Label(self.top_frame,text=‘Winken‘)
self.label2 = Tkinter.Label(self.top_frame,text=‘Blinken‘)
self.label3 = Tkinter.Label(self.top_frame,text=‘Nod‘)
self.label1.pack(side=‘top‘)
self.label2.pack(side=‘top‘)
self.label3.pack(side=‘top‘)
self.label4 = Tkinter.Label(self.bottom_frame,text=‘Winken‘)
self.label5 = Tkinter.Label(self.bottom_frame,text=‘Blinken‘)
self.label6 = Tkinter.Label(self.bottom_frame,text=‘Nod‘)
self.label4.pack(side=‘left‘)
self.label5.pack(side=‘left‘)
self.label6.pack(side=‘left‘)
self.top_frame.pack()
self.bottom_frame.pack()
Tkinter.mainloop()
mygui = MyGUI()
按钮Widget和信息对话框
使用tkMessageBox模块的showinfo函数来显示信息对话框。
例:
代码如下:
# the program demonstrates a Button widget.
# when the user clicks the button, an info dialog box is displayed.
import Tkinter
import tkMessageBox
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.my_button = Tkinter.Button(self.main_window, text=‘Click me!‘,command=self.do_something)
self.my_button.pack()
Tkinter.mainloop()
def do_something(self):
tkMessageBox.showinfo(‘Response‘,‘Thanks for clicking the button.‘)
mygui = MyGUI()
例2:
代码如下:
import Tkinter
import tkMessageBox
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.my_button = Tkinter.Button(self.main_window, text=‘Click me!‘,command=self.do_something)
self.quit_button = Tkinter.Button(self.main_window,text=‘Quit‘,command=self.main_window.quit)
self.my_button.pack()
self.quit_button.pack()
Tkinter.mainloop()
def do_something(self):
tkMessageBox.showinfo(‘Response‘,‘Thanks for clicking the button.‘)
mygui = MyGUI()
用Entry Widget得到输入
Entry Widget是一个矩形区域,用户可输入其中,
可使用Entry Widget的get方法取回输入的数据。
例:
代码如下:
import Tkinter
import tkMessageBox
class KiloGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.top_frame. = Tkinter.Frame(self.main_window)
self.bottom_frame. = Tkinter.Frame(self.main_window)
self.label = Tkinter.Label(self.top_frame,text=‘Enter a distance in kilometers:‘)
self.entry = Tkinter.Entry(self.top_frame,width=10)
self.button1 = Tkinter.Button(self.bottom_frame,text=‘Convert‘,command=self.convert)
self.button2 = Tkinter.Button(self.bottom_frame,text=‘Quit‘,command=self.main_window.quit)
self.label.pack(side=‘left‘)
self.entry.pack(side=‘left‘)
self.button1.pack(side=‘left‘)
self.button2.pack(side=‘left‘)
self.top_frame.pack()
self.bottom_frame.pack()
Tkinter.mainloop()
def convert(self):
kilo = float(self.entry.get())
miles = kilo*0.6214
tkMessageBox.showinfo(‘Result‘,str(kilo)+‘ kilometers is equal to ‘+str(miles)+‘ miles.‘)
mygui = KiloGUI()
例2:
代码如下:
import Tkinter
import tkMessageBox
class KiloGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.top_frame. = Tkinter.Frame(self.main_window)
self.mid_frame. = Tkinter.Frame(self.main_window)
self.bottom_frame. = Tkinter.Frame(self.main_window)
self.label1 = Tkinter.Label(self.top_frame,text=‘Enter a distance in kilometers:‘)
self.entry = Tkinter.Entry(self.top_frame,width=10)
self.button1 = Tkinter.Button(self.bottom_frame,text=‘Convert‘,command=self.convert)
self.button2 = Tkinter.Button(self.bottom_frame,text=‘Quit‘,command=self.main_window.quit)
self.label2 = Tkinter.Label(self.mid_frame,text=‘Converted to miles:‘)
self.value = Tkinter.StringVar()
self.label3 = Tkinter.Label(self.mid_frame,textvariable=self.value)
self.label1.pack(side=‘left‘)
self.entry.pack(side=‘left‘)
self.button1.pack(side=‘left‘)
self.button2.pack(side=‘left‘)
self.label2.pack(side=‘left‘)
self.label3.pack(side=‘left‘)
self.top_frame.pack()
self.mid_frame.pack()
self.bottom_frame.pack()
Tkinter.mainloop()
def convert(self):
kilo = float(self.entry.get())
miles = kilo*0.6214
self.value.set(miles)
mygui = KiloGUI()
Radio按钮和Check按钮
例:
代码如下:
import Tkinter
import tkMessageBox
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.top_frame. = Tkinter.Frame(self.main_window)
self.bottom_frame. = Tkinter.Frame(self.main_window)
self.radio_var = Tkinter.IntVar()
self.radio_var.set(1)
self.rb1 = Tkinter.Radiobutton(self.top_frame,text=‘Option 1‘,variable=self.radio_var,value=1)
self.rb2 = Tkinter.Radiobutton(self.top_frame,text=‘Option 2‘,variable=self.radio_var,value=2)
self.rb3 = Tkinter.Radiobutton(self.top_frame,text=‘Option 3‘,variable=self.radio_var,value=3)
self.rb1.pack()
self.rb2.pack()
self.rb3.pack()
self.ok_button = Tkinter.Button(self.bottom_frame,text=‘OK‘,command=self.show_choice)
self.quit_button = Tkinter.Button(self.bottom_frame,text=‘QUIT‘,command=self.main_window.quit)
self.ok_button.pack(side=‘left‘)
self.quit_button.pack(side=‘left‘)
self.top_frame.pack()
self.bottom_frame.pack()
Tkinter.mainloop()
def show_choice(self):
tkMessageBox.showinfo(‘Selection‘,‘You selected optioin ‘+str(self.radio_var.get()))
mygui = MyGUI()
作者:企鹅不笨 字体:[增加 减小] 类型:
具体如下:
下面的代码可以让字符串居中,左对齐和右对齐,字符串长度设置为50,居中后左右补充空格,右对齐会在左侧补充空格
string1 = “Now I am here.”print string1.center( 50 )print string1.rjust( 50 )print string1.ljust( 50 )
希望本文所述对大家的Python程序设计有所帮助。
具体方法:
1、在桌面上单机右键,选择个性化;
2、点击左侧的锁屏界面菜单;
3、将窗口滚动到最下方,点击屏幕保护程序设置;
4、在弹出新的窗口中设置,与win7和xp中的方法一样。
[Win10屏幕保护程序在哪里设置_设置方法]