python tkinter 布局中overrideredirect使用时能不能不隐藏进程

Python tkinter screen width and height secondary display - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I have a dual monitors set up (laptop screen and external monitor). My laptop screen is my primary display and external monitor is secondary. Both have different screen sizes.
In my python tkinter script, i have used winfo_screenwidth() and winfo_screenheight() to detect the screen width and height so that I can set the root window to become full screen.
Normally, when I run my script, the root window will be the size of my laptop screen. When i move the window to my extended monitor, I want it to auto adjust the width and height to match the external display's width and height.
Is this possible? Is there any way to check if the root window is in primary or secondary display?
Does winfo_screenwidth() detect the width and height of the secondary display?
EDIT: I am using Windows XP Pro 32 bit & Python 2.7.
1,43193673
How I did it:
t = Tk() # new window
t.update()
t.attributes("-alpha", 00)
t.state('zoomed') # maximize the window
height= t.winfo_height() # ...
width= t.winfo_width()
But sadly I do not know of the location of the other screen.
But I think you can do this
create a new window
use winfo_screenheight() and winfo_screenwidth() to find out about the original screen
use geometry() to move the window around
maximize the window (it should always maximize at the screen where it is)
get geometry()
if geometry is at (0, 0) it is the main screen, proceed with 3.
you found another screen
8,27711233
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledfrom Tkinter import *
class Application(Frame):
def __init__(self,master=None, *args, **kwargs):
Frame.__init__(self,master, *args, **kwargs)
self.grid()
self.createWidgets()
self.flag=True
self.transparent=False
self.top = self.winfo_toplevel()
def overturn(self):
self.top.update_idletasks()
self.top.overrideredirect(self.flag)
self.flag=not self.flag #switch
def createWidgets(self):
self.canvas = Canvas(self, bg='green')
self.canvas.pack()
self.flagButton = Button(self, text='try this', bg='green', command=self.overturn)
self.flagButton.pack()
app = Application()
app.master.title("sample application")
app.mainloop()
我唯一想到的办法就是你试试不用frame,而是用button作为父窗口。
另外relief 设置成FLAT,
bd设置为0, 不显示标题的办法我也没有。
不过可以从win32的API上想办法。
获取窗口句柄可以用FindWindow来做到。获取windows的handle后。设置windows的style。
在dwStyle里将WS_CAPTION属性去掉。SetWindowLong这个函数应该有这个功能。
转自百度知道。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:44159次
积分:2370
积分:2370
排名:第11318名
原创:207篇自从接触python,就被他优雅而简洁的代码所吸引。
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ]#如果想要将arr中大于10的项取出来,然后每项乘以2,组成一个新的列表arr2,在python里只需要这么一样就行了:arr2 = [x * 2 for x in arr if x & 10 ]
如果用其他语言来写的吗,不会这么简洁,美观。
python还有更强大的特性,它有非常丰富的类库,以至于它几乎能完成大多数语言都能完成的工作。比如写网站、写软件、写游戏、写服务器、嵌入式开发等。
最近出门总是天公不作美,仔细想想是忘了看天气预报了。于是就想找个桌面天气软件,这样一打开电脑就能看到天气了,关电脑前也可以看一眼明天的天气。
在百度里面找到了好几款桌面天气软件,大都不完美,不是自己想要的效果(有的是界面不美观,有的是里面夹杂着其他程序过于累赘,有的是快捷键冲突了,有的是只能看当前天气不能看明天天气等等)。
当然也有一款个人感觉挺满意的。后来想了下,既然python这么强大,那么用python写一个这样的软件岂不是更好,想要什么功能,就写什么功能。
于是就开始了我的python桌面天气预报的庞大工程。(为什么说庞大呢,后面就知道了。)
由于我是做界面开发的,之前学python的时候就顺便学了wxPython这个强大的类库。于是就用wxPython来写。
写之前肯定先要解决几个问题:1、天气数据的获取与分析。2、能够贴在桌面上,鼠标穿透,像桌面背景一样。3、要背景透明。
1、很容易解决网上的api一大堆,可以百度一下。
2、我也找到了办法就是用User32.dll里面的SetWindowLongA方法。具体的python代码是这么写的
selfWnd = ctypes.windll.user32.FindWindowA("wxWindowClassNR", None)Ret = ctypes.windll.user32.GetWindowLongA(selfWnd, -20)Ret = Ret | 0x | 0xctypes.windll.user32.SetWindowLongA(selfWnd, -20, Ret)
3、这个是最麻烦的了,也是浪费我时间最多的一个。
最开始在wxPython里面找到一个叫SetTransparent()的方法,试了下发现是设置整个窗口的透明度的,不是设置背景的透明度的。又想到把一张png图片作为背景图片不就行了吗。
用尽了各种方法后发现即使用png作为窗口的背景,窗口原来的背景也会同时存在的。相当于就是用一张图片浮在窗口背景上了,没达到我要的背景透明的效果。
后来发现wxPython有个ShapedWindow的例子,可以实现用图片作为背景的效果。
代码就不贴了,效果是这样子的
跟我要的效果很接近。如果再把这张图片做成半透明的,那么就是我要的效果了。
于是百度、谷歌狂搜,也没找到个方法来,最后从各种搜索结果中发现了两个问题:
1、这张作为背景的图片是不支持alpha通道的,即使图片有透明度也不行,虽然wxPython有将png图片显示在程序里的例子,但是作为背景的话,是不可能的。
2、这张图片显示区域之外的部分是显示不出来的。如图:
文字其实是有红框那么长,但是由于在这张图片的区域之外,所以就没有显示出来。
后来在网上发现有个用tkinter实现的背景透明为文字不透明的方法,如图:
基本上是我想要的效果。
于是又重燃了我学习tkinter的决心,放弃用wxPython了,之前一直没有研究这个python内置的Gui工具。
放弃用wxPython来写这个程序还有其他方面的原因:
1、不能实现我想要的用透明背景图片作为窗口的背景。
2、wxStaticText这个文本显示控件,是有背景的,默认的背景是从父级继承过来的。网上有办法可以擦除背景,但是对于右对齐的文字来说,在重绘的时候,会出现文字重叠如图:
正常情况下:,重叠的情况:,这个文字被重绘了好几遍。
3、由于要实现鼠标穿透效果,需要获取窗口句柄,而wxPython没有任何一个方法可以返回自己的窗口句柄。
虽然可以通过 selfWnd = ctypes.windll.user32.FindWindowA("wxWindowClassNR", None) 来获取句柄,
但是这个方法不可靠,因为窗口类名wxWindowClassNR不是唯一的,用spy++可以看到有好多窗口的类名都是
这个名字。这种方法会造成,返回的不是自己窗口的句柄,而是别人窗口的句柄。
俗话说 磨刀不误砍柴工,我把tkinter入门,各个组件的介绍,使用方法,例子都看了一遍,都一一测试过,防止学的时候没学好,理解不透彻,导致用的时候碰到各种问题。
n天后,已经学的差不多了,就开始用tkinter来写这个程序。窗口透明效果很容易就实现了。
代码如下:
# -*- coding: utf-8 -*-&import Tkinter&root = Tkinter.Tk()#这一行可以隐藏掉窗口边框和标题栏root.overrideredirect(True)root["width"] = 100root["height"] = 80#这一行可以将所有的白色透明掉root.attributes("-transparentcolor","white")root["background"] = "white"&x, y = 0, 0&def StartMove(event):&&&&global x, y&&&&x = event.x&&&&y = event.ydef StopMove(event):&&&&x = None&&&&y = Nonedef OnMotion(event):&&&&global x, y&&&&deltax = event.x - x&&&&deltay = event.y - y&&&&_x = root.winfo_x() + deltax&&&&_y = root.winfo_y() + deltay&&&&root.geometry("+%s+%s" % (_x, _y))&&label = Tkinter.Label(root, text="ddddd", bg="white")label.place(x=10, y=10, anchor=Tkinter.NW)&root.bind("&ButtonPress-1&", StartMove)root.bind("&ButtonRelease-1&", StopMove)root.bind("&B1-Motion&", OnMotion)&root.mainloop()
这是效果图:
&后面的颜色是我的桌面,可以看到,背景完全透明了。文字也没有像wxPython那样被切割掉。
但是有一个很明显的问题,文字变得不清晰了。
这是在win7下的效果但是在xp下就显示正常了,如图:
现在都用win7了 不可能写个程序只能在xp下运行吧。所以得找解决办法。
又百度、谷歌狂搜,最后还是在python自带的文档里找到了这么一句话:
like anti-aliased font rendering under X11, window transparency (on X11 you will need a composition window manager) will be missing.
意思是字体抗锯齿、窗口透明在window下将会丢失。
就说是tkinter里面是没有抗锯齿和窗口透明功能的。之前的透明方法只是把特定颜色透明了,并不是真正意义上的透明。tkinter里面是没有alpha通道的。所以用半透明图片做背景是不可能实现的。
不过tkinter相比wxPython来说有个优点就是它可以返回窗口句柄了,print hex(root.winfo_id())
无奈之下,只好再找其他的python GUI工具了。
接下来装了个pygtk,但是装上之后,导入时会报错,大概意思是缺少某个dll文件。我都是按照教程安装的。
后来在虚拟机的xp系统里面装了下,却没有报错。
真是奇怪了,同样的几个文件,同样的安装顺序,在win7下装完后有问题,在xp下就正常运行。
我估计是某个pygtk用到的dll文件在win7和xp里面不一致导致的。
搜了下发现网上也有人遇到同样情况的,但是基本上找不到解决方案,或者说很难找,于是就暂时放弃了。
之后又装了个pyqt,安装过程也很方便,有个独立安装包,直接安装就行,可以正常运行。
看了里面的例子后顿时感觉:&梦里寻她千百度,蓦然回首,那人却在灯火阑珊处。&
真是太好了,由于我是做actionscript开发的,发现qt几乎能实现flash的所有功能。
再次,磨刀不误砍柴工。先看基础吧。
这个不错,可供大家参考
经过一番努力和研究之后,终于实现了我想要的效果了。
先贴图看下效果:
pyqt里面实现窗口无边框的是:
self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.SubWindow|QtCore.Qt.WindowStaysOnBottomHint)
实现背景透明的是:
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
我写了个函数来实现鼠标穿透的切换:
def toggleMouseThrough(self, through=None):&&&&if through==None:&&&&&&&&self.mouseThrough = not self.mouseThrough&&&&else:&&&&&&&&self.mouseThrough = bool(through)&&&&selfWnd = int(self.winId())&&&&ret = ctypes.windll.user32.GetWindowLongA(selfWnd, -20)&&&&if self.mouseThrough:&&&&&&&&ret = ret | 0x&&&&&&&&self.SetTopTexts(self.miniWeatherArr[0].data)&&&&else:&&&&&&&&ret = ret & ~0x&&&&ctypes.windll.user32.SetWindowLongA(selfWnd, -20, ret)
源码放在百度网盘里了,有想研究的可以看下。
阅读(...) 评论()君,已阅读到文档的结尾了呢~~
python Tkinter编程[指南]
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
python Tkinter编程[指南]
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
python_Tkinter编程
下载积分:400
内容提示:python_Tkinter编程
文档格式:PPT|
浏览次数:13|
上传日期: 08:29:44|
文档星级:
该用户还上传了这些文档
python_Tkinter编程
官方公共微信

我要回帖

更多关于 python tkinter 安装 的文章

 

随机推荐