您尚未登录。

楼主 #1 2019-02-12 09:35:30

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Ubuntu 搭建Python Tk 开发平台

apt-get install python3-pip
apt-get install python3-tk

两行代码建立 Tk 测试程序:

import tkinter
tkinter._test()

离线

楼主 #2 2019-02-12 09:37:50

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: Ubuntu 搭建Python Tk 开发平台

离线

楼主 #3 2019-02-12 15:02:17

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: Ubuntu 搭建Python Tk 开发平台

https://stackoverflow.com/questions/17843596/difference-between-tkinter-and-tkinter

For python2 it is:

from Tkinter import *

For python3 it is:

from tkinter import *

python2/3 这个库的大小写有差异


可以通过这个办法兼容 python2/3:

try:
	import tkinter
	print"importing tkinter from python 3.x"
except:
	import Tkinter
	print"importing Tkinter from python 2.x"
finally:
	print"Difference !"

离线

楼主 #4 2019-02-12 16:04:11

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: Ubuntu 搭建Python Tk 开发平台

import tkinter

root = tkinter.Tk()
root.title("hello title")
root.geometry('800x480')

frame = tkinter.Frame(root, bg='black')

canvas = tkinter.Canvas(root, width=320, height=240, bg="blue")
canvas.focus_set()

canvas.create_line(100, 100, 300, 400, fill="red");

canvas.pack(expand=True)
#canvas.bind("<B1-Motion>")

root.mainloop()

20190212160436.png

离线

楼主 #10 2019-02-12 17:30:58

pythinker
会员
注册时间: 2019-02-12
已发帖子: 215
积分: 215

Re: Ubuntu 搭建Python Tk 开发平台

from tkinter import *

class App:

	def __init__(self, master):

		frame = Frame(master)
		frame.pack()

		self.button = Button(
			frame, text="QUIT", fg="red", command=frame.quit
			)
		self.button.pack(side=LEFT)

		self.hi_there = Button(frame, text="Hello", command=self.say_hi)
		self.hi_there.pack(side=LEFT)

	def say_hi(self):
		print("hi there, everyone!")
		canvas = Canvas(root, width=100, height=100, bg="blue")
		i = canvas.create_line(10, 10, 30, 80, 50, 120, 70, 320, fill="red");
		canvas.pack(expand=True)
	
root = Tk()
root.geometry('320x240')

app = App(root)

root.mainloop()
root.destroy() # optional; see description below

20190212173016.png

按下按钮,触发绘图.

http://effbot.org/tkinterbook/tkinter-hello-again.htm

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn