我试图在Python 3中使用tkinter创建一个简单的移动块,一切正常,直到我导入时间。窗口现在无法打开。我已经尝试删除导入,但它没有帮助。这是我的代码:
from tkinter import *
import time
canvas_height = 400
canvas_width = 600
canvas_colour = "grey50"
moveBoolean = "True"
def move():
global moveBoolean
while moveBoolean == "True":
time.sleep(0.005)
canvas.move(square, 90, 90)
time.sleep(0.005)
canvas.move(square, 180, 180)
time.sleep(0.005)
canvas.move(square, 50, 100)
window = Tk()
canvas = Canvas(bg=canvas_colour, height=canvas_height, width=canvas_width, highlightthickness=0)
canvas.pack()
square = canvas.create_rectangle(50, 50, 50, 50, width=50, fill="black")
move()
window.mainloop()
答案 0 :(得分:0)
这些问题通常通过使用类来解决,您应该在编写GUI恕我直言之前学习和使用这些类。你不应该使用time(),因为它可以中断无限的Tkinter循环。改为使用Tkinter的after()。此外,您永远不会将moveBoolean设置为False,因此while语句会一直运行,直到程序被取消,第二次通过square将不在画布上,这就是为什么你没有得到任何可见的东西。以下内容可以解决您的问题,但如果使用类结构则会更好。
<?php
error_reporting(0);
include('../includes/header.php');
include('connection.php');
?>
<script src="../jqm/demos/js/jquery.js"></script>
<script type="text/javascript">
var db;
$(document).ready(function () {
loadJobRecords();
}
$(document).on("click", "#jobRecord", function () {
getJobById($(this).data("key"));
});
});
function loadJobRecords() {
db.transaction(function (txs) {
txs.executeSql('SELECT * FROM jobs', [], function (txs, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
var JobRecord = results.rows.item(i);
var individualJob = '';
individualJob = '<li><a href="#detailinfo" id="jobRecord" data-key="' + jobRecord.ID + '" >';
individualJob += '<h3>' + jobRecord.Title + '</h3>';
individualJob += '<p>Testing</p>';
individualJob += '</a></li>';
$('#listofjobs ul').append(individualJob);
$('#listofjobs ul:visible').listview('refresh');
}
});
});
}
function getJobById(id) {
db.transaction(function (txs) {
txs.executeSql('SELECT * FROM jobs WHERE id="' + ID + '"', [], function (txs, results) '){
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
var jobRecord = results.rows.item(i);
var individualjob = '';
individualjob += '<h3>' + jobRecord.Title + '</h3>';
individualjob += '<p>' + jobRecord.Description + '</p>';
individualjob += '<p>' + jobRecord.Username + '</p>';
$('#jobSummary').html(individualJob);
}
});
});
}
</script>
<div class="content container">
<div daa-role="page" id="jobsdatabase">
<section>
<div data-role="content" id="listofjobs">
<ul data-role="listview" data-filter="true" data-inset="true">
</ul>
</section>
</div>
</div>
</div>
<?php include('../includes/footer.php'); ?>